The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Assign NULL to an array
Discuss Assign NULL to an array in the C Programming forum on Dev Shed. Assign NULL to an array C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 12th, 2012, 11:34 AM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 112
Time spent in forums: 1 Day 4 h 12 m 48 sec
Reputation Power: 1
|
|
|
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!
|

December 12th, 2012, 12:02 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
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.
|

December 12th, 2012, 12:54 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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.
|

December 12th, 2012, 11:56 PM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 112
Time spent in forums: 1 Day 4 h 12 m 48 sec
Reputation Power: 1
|
|
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'.
|

December 13th, 2012, 12:36 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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.
|

December 13th, 2012, 01:30 AM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 31
Time spent in forums: 8 h 5 m 22 sec
Reputation Power: 1
|
|
|
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;
}
|

December 13th, 2012, 01:55 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: | 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?
|

December 13th, 2012, 10:47 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
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) ) ;
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|