if ( is_variable_initialized)

This is a discussion on if ( is_variable_initialized) within the C Programming forums, part of the General Programming Boards category; Hi all! Code: struct classe { char name[50]; int start; int end; } tipo[500]; ... if ((tipo[c].start) || (d==1)) { ...

  1. #1
    ipe
    ipe is offline
    Registered User
    Join Date
    Jan 2003
    Posts
    52

    if ( is_variable_initialized)

    Hi all!

    Code:
    struct classe
    {
    	char name[50];
    	int start;
    	int end;
    } tipo[500];
    
    ...
    
    if ((tipo[c].start) || (d==1))
    {
    	tipo[c].end = a-1;
    	tipo[++c].start = a;
    	d=0;
    }
    else
    {
    	tipo[c].start = a;
    	d=1;
    }
    	strcpy(tipo[c].name, hst[a]);
    	a--; b++;
    Well in this code I'm trying to know if tipo[c].start is already started. Beacuse if c = 0 and a = 0 it'll consider not tipo[c].start ( tipo[c].start = 0 ); But I need to know just if tipo[c].start is already started, not if its value is null. For this reason I created d=0 / d = 1, but didn't worked.
    Someone could help me to know if the variables is already initialized?

  2. #2
    ipe
    ipe is offline
    Registered User
    Join Date
    Jan 2003
    Posts
    52
    actually, is working with d=0 / d=1 (i checked again). But this is not functional. I really would like to know how if the variable is already initializaed and then d=0 / d=1 will be unnecessary.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Someone could help me to know if the variables is already initialized?
    You have to control this yourself through the code.

    Why not just initailise the data in the structure in the first place. This might do for you (or write a for loop to enter specific values to each element)
    Code:
    struct classe
    {
    	char name[50];
    	int start;
    	int end;
    } tipo[500] = {0};
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    ipe
    ipe is offline
    Registered User
    Join Date
    Jan 2003
    Posts
    52
    How come? If I initialize it the "if clause" won't work when c = 0

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I'm not really understanding what you want. I was suggesting that you initialise the array before actually using it in the main code.

    Can you rephrase your question?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    ipe
    ipe is offline
    Registered User
    Join Date
    Jan 2003
    Posts
    52
    look at the code.

    Code:
    for (a; a < qt_hst-b; a++)
    {
    	fgets(hst[a], 99, arq);
    	if (strlen(hst[a]) == 1) {a--; b++;}
    	else
    	{
    		if (strchr(hst[a],'#'))
    		{
    			if (a == tipo[c].start && c != 0) c--;
    			if ((tipo[c].start) || (d==1))
    			{
    				tipo[c].end = a-1;
    				tipo[++c].start = a;
    				d=0;
    			}
    			else
    			{
    				tipo[c].start = a;
    				d=1;
    			}
    			strcpy(tipo[c].name, hst[a]);
    			a--; b++;
    		}
    	}
    }

    suppose that:

    tipo[0].start = 0;
    then at the next loop it will ignore that tipo[0].start is already initialized before. For this reason I'm using d=0 / d=1

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, I think I'm getting you now

    You cannot determine if a variable has been initialised or not by testing it directly. By this, I mean that an int variable will always have an int value. If you don't assign it one specifically, you'll get whatever is in memory when the variable is created, which will still be an int, just probably not in the range you're expecting.

    Somehow, you must control the logic yourself. Using d=0/d=1 you have already done this. There are other ways I can think of:

    How about initialising the start variable to -1 on all array elements. Will that help you? You could test for -1, providing it is not a valid number as far as the data goes.

    You could have another structure element like this:
    Code:
    struct classe
    {	
        char name[50];	
        int start;	
        int end;
        int init;
    } tipo[100] = {0};
    The init variable will be set to 0, then you can change it to 1 when you've used that array element. This would have the same affect as using d=0/d=1.

    Alternatively, rethink the logic and rewrite the code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    ipe
    ipe is offline
    Registered User
    Join Date
    Jan 2003
    Posts
    52
    I changed the struct as you suggested.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21