2
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!

asked Nov 7, 2010 at 16:48
10
  • 6
    +1 for coining the term "pointering" :) Commented Nov 7, 2010 at 16:50
  • 1
    Why are you multiplying the size by colunas and linhas? This will give you an array of structs, not just one. Commented Nov 7, 2010 at 16:55
  • Because I need an array of structs :P Commented Nov 7, 2010 at 16:56
  • 4
    We had The Riddler, now we have The Pointerer. I vote that all pointer related questions should subsequently begin with POINTER ME THIS! Commented Nov 7, 2010 at 17:14
  • 1
    I meant use calloc in main ;-). Commented Nov 7, 2010 at 17:28

4 Answers 4

5

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);
answered Nov 7, 2010 at 16:53
Sign up to request clarification or add additional context in comments.

1 Comment

Most of the answers were pretty good and worked. I just choose the one that had more upvotes. Thanks everyone for the help.
3

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;
}
answered Nov 7, 2010 at 17:12

3 Comments

Why the assertion instead of explicit 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.
@Tim: +1 ... I just added the assert (and the free) because I always say "check the return value of malloc" and I had failed to do that in this post. Thank you for pointing it out; I'm editing the post
upvoted, this answer took time and is the most helpful thus far. +2 if I could, as you made your example match the comments under it :)
0

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 
answered Nov 7, 2010 at 17:09

Comments

0

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);
answered Nov 7, 2010 at 16:59

1 Comment

@Queops, sorry for the delay, I have edited the answer accordingly.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.