0

I would like to do something like:

struct mystruct {
   char *info;
};

// here is where I'm not sure how to
void do_something(struct mystruct **struc){
    int i;
    for (i = 0; i < 10; i++){
       *struc[i] = (struct mystruct *) malloc (sizeof (struct mystruct));
       *struc[i]->info = "foo";
    } 
}
int main(int argc, char *argv[]){
    struct mystruct **struc;

    struc = (struct mystruct **struc) malloc (sizeof(struct mystruct *struc) * 10);

    dosomething(&struc);
    // do something with struc and its new inserted values
    return 0;
}

I'm not sure how to pass it as a reference so I can make use of it after dosomething()

Thanks

3
  • That shouldn't compile. You're passing a struct mystruct *** into a function that takes a struct mystruct **. That aside, what's the problem? All the values should be accessible to you after the function returns. Commented Nov 6, 2010 at 4:31
  • @EboMike: you are right, is not compiling, and that's my question, I guess I'm getting confused on how to handle properly the *, ***. Commented Nov 6, 2010 at 4:35
  • void do_something(struct mystruct **struc) should take ***struc as its argument, I think. Commented Nov 6, 2010 at 4:40

3 Answers 3

2

Ok, here is my corrected version. Specifically...

Line 26: no reason to cast the result of malloc(3), it already returns a void *

Line 28: don't make a pointless triple-indirect pointer by passing &struc, you have already allocated space for it so it's hard to imagine any possible reason to change it. You want ultimately to pass the exact return value from malloc(3) down to the next layer.

Line 11: another unnecessary cast, and we really do want to change the row pointer at struct[i], i.e., *struc[i] would change what one of those 10 pointers that main() allocated points to, but they haven't been set yet. That's the job here.

And with those changes it works pretty well...

 1  #include <stdio.h>
 2  #include <stdlib.h>
 3  
 4  struct mystruct {
 5    char *info;
 6  };
 7  
 8  void do_something(struct mystruct ** struc) {
 9    int i;
10    for (i = 0; i < 10; i++) {
11      struc[i] = malloc(sizeof(struct mystruct));
12      struc[i]->info = "foo";
13    }
14  }
15  
16  void do_something_else(struct mystruct ** s) {
17    int i;
18  
19    for (i = 0; i < 10; ++i)
20      printf("%2d: %s\n", i, s[i]->info);
21  }
22  
23  int main(int argc, char *argv[]) {
24    struct mystruct **struc;
25  
26    struc = malloc(sizeof(struct mystruct *) * 10);
27  
28    do_something(struc);
29    do_something_else(struc);
30    return 0;
31  }
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of dosomething(&struc);, use dosomething(struc);. You have a struct mystruct **, and that's what the function expects.

Instead of

*struc[i] = (struct mystruct *) malloc (sizeof (struct mystruct));

Use

struc[i] = (struct mystruct *) malloc (sizeof (struct mystruct));

struc is a struct mystruct **, so struc[i] will expect a struct mystruct *.

Comments

0

Consider not casting malloc since it is void *:

http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1047673478&id=1043284351

Comments

Your Answer

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.