struct aPoint {
int somaVertical;
int somaHorizontal;
int valor;
};
I have an array of structs dynamically created in main(), like so:
struct aPoint *ps = malloc( sizeof(struct aPoint) * columns * rows )
And I want to work with its struct values outside of main() in a function that has sscanf(). The initialization of the array is also taken care of on the main().
How can I pass the array of structs through that function and set some struct values of it aswell? Argh I hate pointering!
Thanks!
-
6+1 for coining the term "pointering" :)Doug T.– Doug T.2010年11月07日 16:50:51 +00:00Commented Nov 7, 2010 at 16:50
-
1Why are you multiplying the size by colunas and linhas? This will give you an array of structs, not just one.user325117– user3251172010年11月07日 16:55:23 +00:00Commented Nov 7, 2010 at 16:55
-
Because I need an array of structs :PQosmo– Qosmo2010年11月07日 16:56:49 +00:00Commented Nov 7, 2010 at 16:56
-
4We had The Riddler, now we have The Pointerer. I vote that all pointer related questions should subsequently begin with POINTER ME THIS!user50049– user500492010年11月07日 17:14:36 +00:00Commented Nov 7, 2010 at 17:14
-
1I meant use calloc in main ;-).user325117– user3251172010年11月07日 17:28:50 +00:00Commented Nov 7, 2010 at 17:28
4 Answers 4
That would be:
int readStuff(struct aPoint *ps, size_t len, const char *someVar)
{
unsigned int i;
for (i = 0; i < len; i++) {
sscanf(someVar, "%d", &(ps[i].somaVertical));
/* And so on for the other fields */
}
/* Return whatever you're returning here */
}
const size_t len = colunas * linhas;
struct aPoint *ps = calloc(len, sizeof(struct aPoint));
int success = readStuff(ps, len, arrayOfNumbers);
1 Comment
This works for me
/* #include <assert.h> */
#include <stdio.h>
#include <stdlib.h>
struct aPoint {
int somaVertical;
int somaHorizontal;
int valor;
};
int readStuff(struct aPoint *data, int rows, int cols) {
sscanf("42", "%d", &data[3].somaVertical);
sscanf("142", "%d", &data[3].somaHorizontal);
sscanf("-42", "%d", &data[3].valor);
return 0;
}
int main(void) {
struct aPoint *ps;
int colunas, linhas;
colunas = 80;
linhas = 25;
ps = malloc(sizeof *ps * colunas * linhas);
/* assert(ps); */ /* thanks Tim */
if (ps) {
readStuff(ps, linhas, colunas);
printf("%d %d %d\n", ps[3].somaVertical, ps[3].somaHorizontal, ps[3].valor);
free(ps);
} else {
fprintf(stderr, "no memory.\n");
exit(EXIT_FAILURE);
}
return 0;
}
3 Comments
malloc() failure checking? You know what malloc() returns when it fails. Why assert unless checking a condition that could happen beyond the return of a function? For instance, assert(i != 3); .. Your example means something like NDEBUG turns off checking the failure of malloc(), which is why assertions are sometimes categorized as evil.I think you need either
readStuff(ps);
...
sscanf(someVar, "%d", &(ps[index].valor)); // using index in readStuff
or
readStuff(ps + index); // using index in main
...
sscanf(someVar, "%d", &(ps->valor)); // or &ps[0].valor, that's equivalent
Comments
All functions in C are passed arguments by value, so you can pass a pointer to the struct array you wish to modify:
int readStuff(struct aPoint *p, int numStruct)
{
...
for(i=0; i<numStruct; i++)
{
sscanf(someVar, "%d", &(*(p+i).valor) );
}
...
}
You can call this function with:
struct aPoint *ps = malloc( sizeof(struct aPoint) * columns * rows );
...
readStuff(ps, columns * rows);