473,395 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Avoiding used uninitialized in this function warning

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

Jun 27 '08 #1
1 12213
ma******@gmail.com <ma******@gmail.comwrote:
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
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of...
2
by: Liang | last post by:
Hi, I use "defined $r_libs->{$name}" to check first if a key exists in a hash table. But Perl gives a warning WHENEVER the key exists: "Use of uninitialized value". Would u please help to...
2
by: Ivo | last post by:
In an Win/IE-only application, some checkboxes and the like have been assigned accesskeys, so pressing alt+w focuses a checkbox, alt+b another. This is very convenient. However, when the checkboxes...
19
by: hpy_awad | last post by:
What should I do to avoid the message : parameter 'argc' is never used My program is down : *-------------------- /* Book name : The prodessional programmers guide to C File name ...
5
by: Michael | last post by:
Hi, once I read here that it is not 'a good idea' to pass variables that are not initialized to a function. I have void something ( double *vector ); ....
6
by: Bill Waddington | last post by:
Long time lurker, 1st time poster. I hope this is sufficiently c related. If not, send me to my room. I'm porting a driver to a 64-bit platform. I get handed a struct: typedef struct { ......
40
by: Dave Hansen | last post by:
Please note crosspost. Often when writing code requiring function pointers, it is necessary to write functions that ignore their formal parameters. For example, a state machine function might...
9
by: liljencrantz | last post by:
Hi, I have a piece of code that uses hashtables to store pointers to various bits of data. The hashtable sees all pointers as const void *, while the application obviously uses various other...
17
by: tshad | last post by:
In VS 2008, I have an object, dbReader, that I get a warning saying that it is used before it has been assigned a value. That is correct. Dim dbReader As SqlDataReader and later:
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.