When you declare an array of chars and store a string in it, where is
the position of the null character \0? And what happens to the unused
memory locations?
#include <stdio.h>
int main(void)
{
char gstring2[25] = "dudes";
gstring2[5] = 'a';
printf("%s \n", gstring2);
return 0;
}
The output of main function was
dudesa
How come this code works, and the statement
gstring2[5] = 'a';
doesn't overwrite the null character? 8 2422
Gary wrote:
When you declare an array of chars and store a string in it,
where is the position of the null character \0?
Wherever you ask it to be, if you ask there to be one.
And what happens to the unused memory locations?
Uninitialised elements of a struct or array object will
be 'zero initialised'.
#include <stdio.h>
int main(void)
{
char gstring2[25] = "dudes";
Same as...
char gstring2[25] =
{ 'd', 'u', 'd', 'e', 's',
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
gstring2[5] = 'a';
printf("%s \n", gstring2);
return 0;
}
The output of main function was
dudesa
How come this code works, and the statement
gstring2[5] = 'a';
doesn't overwrite the null character?
It does overwrite the null character, which was followed by
another one.
You need to be careful though of situations like...
char foo[5] = "dudes";
C, unlike C++, allows such an initialisation. There is no
terminating null stored as there is no room for it.
--
Peter
Gary wrote:
When you declare an array of chars and store a string in it, where is
the position of the null character \0?
After the last character of string data.
And what happens to the unused
memory locations?
They are set to zero.
#include <stdio.h>
int main(void)
{
char gstring2[25] = "dudes";
gstring2[5] = 'a';
printf("%s \n", gstring2);
return 0;
}
The output of main function was
dudesa
You overwrote the null terminator with a character.
How come this code works,
Because there were extra characters that were set to zero due to the
partial initialization.
and the statement
gstring2[5] = 'a';
doesn't overwrite the null character?
It did, but a new terminator was in place.
If you had this:
char str[] = "dudes";
Then you'd probably be in trouble. The next adjacent byte wouldn't even
belong to your object so overwriting the null terminator would cause
undefined behavior as soon as you tried a string operation.
Brian
On Mon, 05 May 2008 15:17:44 -0700, Peter Nilsson wrote:
You need to be careful though of situations like...
char foo[5] = "dudes";
C, unlike C++, allows such an initialisation. There is no
terminating null stored as there is no room for it.
yes and you get garbage on the screen:
#include <stdio.h>
int main( void )
{
char oye[2] = "ok";
printf("%s\n", oye);
return 0;
}
============= OUTPUT =============
/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra test.c
/home/arnuld/programs/C $ ./a.out
okHßÿ¿3.L
/home/arnuld/programs/C $
it *accidentally* terminated because at some random place in memory it
found the NULL ?
-- http://lispmachine.wordpress.com/
my email ID is @ the above blog.
just check the "About Myself" page :)
"arnuld" <su*****@see.sigs.invalidha scritto nel messaggio
news:pa****************************@see.sigs.inval id...
>On Mon, 05 May 2008 15:17:44 -0700, Peter Nilsson wrote:
>You need to be careful though of situations like...
char foo[5] = "dudes";
C, unlike C++, allows such an initialisation. There is no terminating null stored as there is no room for it.
yes and you get garbage on the screen:
#include <stdio.h>
int main( void )
{
char oye[2] = "ok";
is it not better oye[4] = "ok"; ?
"ok" is o+k+\0
printf("%s\n", oye);
return 0;
}
============= OUTPUT =============
/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra test.c
/home/arnuld/programs/C $ ./a.out
okHßÿ¿3.L
here someone can see that "\0" is not copied from "ok" to oye[2]
/home/arnuld/programs/C $
it *accidentally* terminated because at some random place in memory it
found the NULL ?
-- http://lispmachine.wordpress.com/
my email ID is @ the above blog.
just check the "About Myself" page :)
arnuld wrote:
On Mon, 05 May 2008 15:17:44 -0700, Peter Nilsson wrote:
You need to be careful though of situations like...
char foo[5] = "dudes";
C, unlike C++, allows such an initialisation. There is no
terminating null stored as there is no room for it.
yes and you get garbage on the screen:
printf("%s\n", oye);
No, you get undefined behavior.
Brian
"rio" <a@b.cwrites:
"arnuld" <su*****@see.sigs.invalidha scritto nel messaggio
news:pa****************************@see.sigs.inval id...
>>On Mon, 05 May 2008 15:17:44 -0700, Peter Nilsson wrote:
>>You need to be careful though of situations like...
char foo[5] = "dudes";
C, unlike C++, allows such an initialisation. There is no terminating null stored as there is no room for it.
yes and you get garbage on the screen:
#include <stdio.h>
int main( void ) { char oye[2] = "ok";
is it not better oye[4] = "ok"; ?
"ok" is o+k+\0
Not to illustrate the point, no. Both Peter Nilsson and Arnuld posted
code that has an array that is not a string, and in both cases it was
deliberate.
If you want to fix the problem, it is hard to beat 'char oye[] = "ok";'
since you then don't need a size. If you must have a size, anything
other than 3 will be mildly confusing in this example.
>-- http://lispmachine.wordpress.com/
<snip>
Best to trim your replies a bit more. In particular, remove sig blocks
unless you are commenting on them.
--
Ben.
"arnuld" <su*****@see.sigs.invalidha scritto nel messaggio
news:pa****************************@see.sigs.inval id...
>On Mon, 05 May 2008 15:17:44 -0700, Peter Nilsson wrote:
>You need to be careful though of situations like...
char foo[5] = "dudes";
C, unlike C++, allows such an initialisation. There is no terminating null stored as there is no room for it.
yes and you get garbage on the screen:
#include <stdio.h>
int main( void )
{
char oye[2] = "ok";
is it not better oye[4] = "ok"; ?
"ok" is o+k+\0
printf("%s\n", oye);
return 0;
}
============= OUTPUT =============
/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra test.c
/home/arnuld/programs/C $ ./a.out
okHßÿ¿3.L
here someone can see that "\0" is not copied from "ok" to oye[2]
/home/arnuld/programs/C $
it *accidentally* terminated because at some random place in memory it
found the NULL ?
-- http://lispmachine.wordpress.com/
my email ID is @ the above blog.
just check the "About Myself" page :)
"arnuld" <su*****@see.sigs.invalidha scritto nel messaggio
news:pa****************************@see.sigs.inval id...
>On Mon, 05 May 2008 15:17:44 -0700, Peter Nilsson wrote:
>You need to be careful though of situations like...
char foo[5] = "dudes";
C, unlike C++, allows such an initialisation. There is no terminating null stored as there is no room for it.
yes and you get garbage on the screen:
#include <stdio.h>
int main( void )
{
char oye[2] = "ok";
is it not better oye[4] = "ok"; ?
"ok" is o+k+\0
printf("%s\n", oye);
return 0;
}
============= OUTPUT =============
/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra test.c
/home/arnuld/programs/C $ ./a.out
okHßÿ¿3.L
here someone can see that "\0" is not copied from "ok" to oye[2]
/home/arnuld/programs/C $
it *accidentally* terminated because at some random place in memory it
found the NULL ?
-- http://lispmachine.wordpress.com/
my email ID is @ the above blog.
just check the "About Myself" page :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Simon |
last post by:
Hi,
If I have a string, (variable len), and I am looking for the first position
of one char in array starting from position 'x'
For example,
// the 'haystack'
$string = "PHP is great,...
|
by: ali |
last post by:
Hi,
I'm trying to understand the reason for different output on the
following codes
Code1:
#include <iostream.h>
int main()
|
by: Durgesh Sharma |
last post by:
Hi All,
Pleas help me .I am a starter as far as C Language is concerned .
How can i Right Trim all the white spaces of a very long (2000 chars)
Charecter string ( from the Right Side ) ? or how...
|
by: JS |
last post by:
I have this struct:
int main()
{
struct jb {
char actor;
struct jb *next;
};
struct jb *bond;
|
by: melanieab |
last post by:
Hi,
I'm trying to store all of my data into one file (there're about 140 things
to keep track of). I have no problem reading a specific string from the
array file, but I wasn't sure how to...
|
by: sandy |
last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory',
within my class Directory (Can you already smell disaster?)
Each Directory can have subdirectories so I thought to put these...
|
by: Lonifasiko |
last post by:
Hi,
Just want to replace character at index 1 of a string with another
character. Just want to replace character at that position. I thought
Replace method would be overloaded with an index...
|
by: Chronictank |
last post by:
Hi, as a bit of background (and seeing as it is my first post :)) i am a complete newbie at perl. I literally picked it up a week ago to do this project as it seemed like the best choice for a...
|
by: qglyirnyfgfo |
last post by:
I was reading an article regarding .Net arrays and on that article,
the author mentioned something about SZ arrays.
As far as I can tell, SZ arrays are one dimension arrays that are zero
based,...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |