C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 12th, 2012, 11:34 AM
so.very.tired so.very.tired is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 112 so.very.tired User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #2  
Old December 12th, 2012, 12:02 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,681 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 2 h 20 m 36 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
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.

Reply With Quote
  #3  
Old December 12th, 2012, 12:54 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,124 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 8 m 53 sec
Reputation Power: 1949
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.

Reply With Quote
  #4  
Old December 12th, 2012, 11:56 PM
so.very.tired so.very.tired is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 112 so.very.tired User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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'.

Reply With Quote
  #5  
Old December 13th, 2012, 12:36 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,124 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 8 m 53 sec
Reputation Power: 1949
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.

Reply With Quote
  #6  
Old December 13th, 2012, 01:30 AM
jaysh4922 jaysh4922 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 31 jaysh4922 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;

}

Reply With Quote
  #7  
Old December 13th, 2012, 01:55 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,124 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 8 m 53 sec
Reputation Power: 1949
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?

Reply With Quote
  #8  
Old December 13th, 2012, 10:47 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,806 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 19 m 38 sec
Reputation Power: 1800
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) ) ;

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Assign NULL to an array

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap