#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2

    Assign NULL to an array


    How can i assign NULL to an array of chars?

    i'm trying to print out a 2D array after storing some data inside, it prints my data plus some garbage it holds in it.

    TIA!
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,762
    Rep Power
    9397
    In case you weren't aware, NULL is a pointer. It is not the \0 character.

    Prints garbage? How are you printing it? Strings should have \0s and numbers should be printed as numbers. In neither case there shouldn't be any garbage.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Not quite sure what you're trying to do, so a code example would help.

    NULL is a pointer. If you're trying to set the array name to NULL, as you would a pointer, then that cannot be done. While array names are for the most part equivalent to pointers, the major difference is that you can never change where the array name is "pointing".

    The '\0' character is sometimes called NUL, originating I believe from old teletype terminology, which is where we got ASCII from. Please note the single "L" and be sure to never confuse it with the NULL pointer.

    There is such a thing as an empty string, which is a string that doesn't contain any characters and hence has a strlen of zero. Its literal is "" . In reality, a C empty string contains a single character, the null-terminator.

    Some examples of assigning empty strings:
    Code:
        char  aString[42];
    
        strcpy(aString, "");
        sString[0] = '\0';
    Was that what you had in mind? Is the situation such that you have an array containing multiple strings, but you only have meaningful strings to assign to some of them, so you want the rest of the strings to be empty strings?

    The printing of garbage is one of the signs that you've left out a null-terminator. If that garbage was from the uninitialized strings that you should have set to be empty strings, then that might be the solution.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    hi.
    thanks for the help.

    let me be more specific, and sorry i didn't point that out earlier.
    here's the relevant code segment:
    Code:
    int main;
    {
    	int i, j=0, k=0;
            char delimiter, input[50], substrings[5][10];
    
            printf("Enter a string\n");
            gets(input);
            printf("Enter a delimiter\n");
            scanf(" %c", &delimiter);
    	for (i=0; input[i]!='\0' ;i++)
    	{
    		if (input[i]!=delimiter)
    		{
    			substrings[j][k]=input[i];
    			k++;
    		}
    		else
    		{
    			k=0;
    			j++;
    		}
    	}
    
            for (i=0; i<4 ; i++)
            {
                  for (j=0; j<9 ;j++)
                        printf("%c", substrings[i][j]);
                  printf("\n");
            }
    }
    it basically takes a string and splits it to substrings.
    it then prints the substrings, and this is where the problem is:
    when the program starts, not all substrings[][] is set to '\0' (some memory blocks contains some weird symbols).
    so i want to set it all to '\0'.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Before using substrings, initialize all its elements to empty strings, what you're calling "set to '\0'" (I'm trying to argue in favor of using standard and consistent terminology, so I'm being a bit pedantic here). I see a few ways of doing it:

    1. the first executable lines in main should be a for-loop that loops through all the rows in substrings and sets them all to empty strings. Think about that for a moment. Do you need to set each and every char element to '\0'? No, you only need to set the first character of each string. Each row represents a single string; you are declaring that there are five strings. Each column is a character within that row's string.

    or 2. Declare substrings with an initialization list. The safe way here would be to explicitly initialize each and every element, but again you only need to initialize the first char of each row; eg:
    Code:
        char substrings[][10] = { "", "", "", "", ""};
    Also, one thing that sticks in my mind is that if you provide an incomplete initialization list, then all the other elements are initialized to zero, which should be equivalent to '\0'. Read up on initialization lists in array declarations to verify this. Or just try it once you have the basic syntax worked out -- I've been taught that the final arbiter of how you can and cannot write code is the compiler itself. Basically, think about this one, read up on it, and play with it; sometimes you need to play a bit with the syntax to get it to work out right.

    or 3. Local variables are not initialized by default, but global variables are initialized to zero by default. This is a trivial solution -- albeit one that you should be intimately familiar with -- and you really should try to the other two first.

    But however you do it, initialize that substrings array before you start using it.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Originally Posted by jaysh4922
    public Int32[] GetIssueTypeCode()

    {


    Int32[] _issueTypeCode =new Int32[Issues.Count] ;
    Int16 _index = 0;

    foreach (Issues item in Issues)
    {
    _issueTypeCode[_index] = item.Issue_Type_code;
    _index++;
    }



    return _issueTypeCode;

    }
    I'm assuming that's C#. We're talking C in this thread.

    Man muß immer an die richtige Sprache denken, nicht wahr?
  12. #7
  13. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,075
    Rep Power
    1802
    If supplied with a single initialiser value, that value will be repeated for the entire array; so:

    Code:
    char input[50] = {'\0'} ;
    char substrings[5][10] = {{'\0'}} ;
    Will initialise all elements in the arrays to zero (the NUL character).

    If you need to reset the data, the easiest way is by using memset():

    Code:
    memset( input, 0, sizeof(input) ) ;
    memset( substrings, 0, sizeof(substrings) ) ;

IMN logo majestic logo threadwatch logo seochat tools logo