Variable used without being initialized Error

Hi everybody,
I am a beginner in C .I see this error many times when I write even little simple programs where I dont initialize the variables :

Run-time check failure - The variable 'mylife' is being used without being initialized.

and the program doesnt runs. for example in following code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main(){
	int c=0;
	struct life {
		int years;
		char** friends;
		char* name;	
		} mylife;
	
	mylife.years=31;
	mylife.name="explore";
	mylife.friends[0]="arash";
	mylife.friends[1]="john";
	mylife.friends[2]="smith";
	

	printf("\nname = %s",mylife.name);
	printf("\nyears = %d",mylife.years);

	 for(c=0;c<3;c++)
		printf("\nFriends %d : %s",c,mylife.friends[c]);

	return 0;
	}


And I cant initialize inside the structure definition.
Can't the structures contain pointer variables?
The problem is, you are not allocating memory for **friends to hold all the values you are trying to assign. Read about dynamic memory allocation and they try this again.
To initialize data members inside the struct you have to use constructor.
And structure CAN contain pointers.

Hope this helps !
Last edited on
> Can't the structures contain pointer variables?

Yes, it can, but as with any pointer you must first point it at something before you can use it. In this case, you are trying to access the array of (char*) [that's the (char*)* friends] without having first allocated memory to it.

1
2
3
4
5
6
7
8
	...
	mylife.years=31;
	mylife.name="explore";
	mylife.friends=(char*)malloc(4*sizeof(char*));
	mylife.friends[0]="arash";
	...
	mylife.friends[3]=NULL;
	...

Notice also how I used the NULL to indicate the end of the life.friends[] array.

Anyway, hope this helps.
1
2
mylife.friends=(char*)malloc(4*sizeof(char*));
mylife.friends[0]="arash";


All right, now this might be a really stupid question, but I am going to ask it anyway. Could you please tell me why we don't have to allocate memory when we do
mylife.friends[0]="arash";
This is same as writing
char *p = "hello"; right?
Shouldn't this be something like
1
2
char *p = (char *) malloc (sizeof(char)*12); // 12 is just random  
strcpy(p,"hello"); 

So when do we have to allocate memory for pointer and when we can do just as char *p = "hello"; , I am really confused in this. Could you please shed some light on this?

Thanks
Last edited on
Thanks guys.

0 - before anything , the code

mylife.friends=(char*)malloc(4*sizeof(char*));

does not compile .it gives the following error :

error C2440: '=' : cannot convert from 'char *' to 'char **'


you should change the cast to (char**) :

mylife.friends=(char**)malloc(4*sizeof(char*));


1 - I know about memory allocation.What confuses me is if I ignore the char** friends; in code and leave the struct only with char* name , the it does compile correctly without any errors. I mean the following code will compile, without allocating memory for char* name :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main(){

	struct life {
		int years;
		//char** friends; // If I comment this it works
		char* name;	// Still not initialized ,but cause no problems.
		} mylife;
	
	mylife.years=31;
	mylife.name="explore";

	printf("\nname = %s",mylife.name);
	printf("\nyears = %d",mylife.years);

	return 0;
	}// Runs withour any errors 


Last edited on
Sorry about missing an asterisk there:
mylife.friends=(char**)malloc(4*sizeof(char*));

As long as a pointer points to memory you own, you can use it:

const char* greeting = "Hello world!";
Valid, as the 'greeting' is pointing to an ASCIIZ array of characters stored in your program's memory space.

const char* crash = 12;
Invalid, as 'crash' is pointing to memory that does not belong to your program.

char* s80 = (char*)malloc( 80 );
Valid, as 's80' is pointing to memory that belongs to your program.

Remember now, that you want an array of strings. A string is an array of characters. So:
1
2
3
4
5
6
7
8
typedef char* string;

struct life
  {
  int     years;
  string* friends;  // Points to one or more 'string's
  string  name;     // A single 'string'
  };

Now when you say mylife.name = "Wilhelmina"; that works, simply because the string (a pointer to one or more chars) now points to the array of chars "Wilhelmina".

However, if you were to say mylife.name[0] = 'A'; then you would have a problem, since 'name' points to some random place in memory (which probably doesn't belong to your program). The attempt to write to anything your program doesn't have rights to will segfault your program.

Before you can dereference any pointer, you must first point it to something you own:
1
2
mylife.name = "Jane";
printf( "%c\n", mylife.name[0] );  /* Works fine, since 'name' points to something you own */


It might be worth your time to read through the tutorials on Compound Data Types
http://www.cplusplus.com/doc/tutorial/

Good luck!
kevinchkin:

When you use string literals in code, the compiler allocates the memory they use. Therefore, you don't have to code for that yourself, and this is why you can do the type of assignations that you see here and there.

For arrays, is a different story because the compiler doesn't have a way of knowing the amount of memory that you are going to need in advance, so it just won't allocate memory for you.

But now that I write this, I think you CAN tell the compiler the array size. I think I have seen constructs like:

1
2
3
4
5
sometype *arrOfSomeType = { val1, val2, val3 }

// Or maybe it was:

sometype arrOfSomeType[] = { val1, val2, val3 }


I think that if you use the syntax above, your program will work. I'd try it myself, but I'm caught with something.
<deleted>
Last edited on
thanks for detailed replies.

I think

const char* crash = 12;

or

const int* crash = 12;

are both a wrong definitions and does not even compile to cause a crash as we are trying to assign an interger to a char* , or int* which only accepts a memory address .

const char* greeting = "Hello world!";

works just because the "Hello World!" string is really in form of an array so when we assign it to char* we are really passing its address (memory location of 'w' ) to the pointer .

Memory belonging to program means memory your program has requested (via compiler from OS) .in case of a string "blahblah" program has to place it in somewhere before it cas use it so it already has an address. [correct me if I am in wrong way]


Topic archived. No new replies allowed.