|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hello,
I am working on upgrading an existent project. I have a method with this the following prototype: void f2(struct myStruct **myStructArray) Now, I need to call a method with the following prototype whenever f2 is called: void f1(struct myStruct *myStructElement) Though f2 get an array, in fact it is **always** called with one element. This is a working assumption on which I can base my work. (though in the beginning of this project it was meant to enable calling this f2 with an array with more than one element). What I tried is this: #include <stdio.h> struct myStruct { int val; }; void f2(struct myStruct **myStructArray) { printf("myStruct->val=%d\n",(*myStructArray)->val ); } void f1(struct myStruct *myStructElement) { struct myStruct **myStructArray; *myStructArray=myStructElement; f2(myStructArray); } int main(int argc, char** argv) { struct myStruct *mystruct = (struct myStruct*)malloc(sizeof(*mystruct)); mystruct->val=1; f1(mystruct); return 0; } Now, when building with -Wall -O2 I get: main.c: In function ‘f1’: main.c:19: warning: ‘myStructArray’ is used uninitialized in this function (without this flags, compilation completes with no warnings). How can I avoid this warning messages when building with -Wall -O2 ? any ideas? Regards, Mark |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
markryde@gmail.com <markryde@gmail.com> wrote:
> Hello, > I am working on upgrading an existent project. > I have a method with this the following prototype: > void f2(struct myStruct **myStructArray) > Now, I need to call a method with the following prototype > whenever f2 is called: > void f1(struct myStruct *myStructElement) > Though f2 get an array, in fact it is **always** called with one > element. > This is a working assumption on which I can base my work. > (though in the beginning of this project it was meant to enable > calling this f2 with an array with more than one element). > What I tried is this: > #include <stdio.h> > struct myStruct > { > int val; > }; > void f2(struct myStruct **myStructArray) > { > printf("myStruct->val=%d\n",(*myStructArray)->val ); > } > void f1(struct myStruct *myStructElement) > { > struct myStruct **myStructArray; 'myStructArray' is a pointer, pointing nowhere useful. > *myStructArray=myStructElement; Now you try to write somethig to the location 'myStructArray' points to. But since you haven't set where 'myStructArray' is supposed to point to it's uninitialized and it points to no place in memory you would be allowed to write to. I guess what you meant to write is myStrArray = &myStructElement; > f2(myStructArray); > } You can simplify this function by just writing void f1( struct myStruct *myStructElement ) { f2( &myStructElement ); } > int main(int argc, char** argv) > { > struct myStruct *mystruct = (struct myStruct*)malloc(sizeof(*mystruct)); You shouldn't cast the return value of malloc(). I guess you put it there since you forgot to include <stdlib.h> (that's where malloc() is declared and it's not included in the code you posted) and then got a compiler warning. The cast can actually be dangerous if you're using a machine where a pointer doesn't fit into an int or which has dedicated address and data registers since without a prototype for malloc() the compiler will assume that it returns an int. > mystruct->val=1; > f1(mystruct); > return 0; > } > Now, when building with -Wall -O2 You probably also should add '-W' to the mix (despite the name '-Wall' does not include all warnings that get produced by '-W'). Regards, Jens -- \ Jens Thoms Toerring ___ jt@toerring.de \__________________________ http://toerring.de |
|
![]() |
| Outils de la discussion | |
|
|