Avoiding used uninitialized in this function warning

M

markryde

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
 
J

Jens Thoms Toerring

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,730
Latest member
AudryNolan

Latest Threads

Top