473,395 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

string in C: confusion

Hi .

According to a reference i am using, ANSI C does not have a built in string type. instead an array of characters need to be used. According to the book, the compiler ends then the array of char with the character zero '\o'. hence if you want to use a string of 4 chars, you need to declare an array of 5 chars.

i wrote a simple code to verify the above
Expand|Select|Wrap|Line Numbers
  1. char m[6]="elvira";
  2. length=strlen(m); 
  3. for( i=0;i<length;i++)
  4. printf(" the chara at order %d os %c\n",i,m[i]);the output was to my suprise
  5.  

Expand|Select|Wrap|Line Numbers
  1.  the chara at order 0 os e
  2.  the chara at order 1 os l
  3.  the chara at order 2 os v
  4.  the chara at order 3 os i
  5.  the chara at order 4 os r
  6.  the chara at order 5 os a
  7.  the chara at order 6 os )
  8.  the chara at order 7 os N
  9.  the chara at order 8 os ├
  10.  
the above execution of the program was unexpected to me. if instead
char m[]="elvira"; is used to declare and initiliase the string, printf outputs
elvira ONLY!

if instead i keep char m[6]="elvira" and then set m[5]='\0', the printf shows only "elvir" as expected. however, if i set m[i]='\0' for any i<6, only the characters up to (i-1) are displayed. The remaining are not displayed and "m" has a shorter length!!!



i am surprised with such executions. i was expecting the array of char to be handled via its subscript like with any other type of array but it seems it is not the case

why ??? they would have better define a string type. any explanations for the above please

Cheers.
Sep 24 '09 #1
5 1772
Markus
6,050 Expert 4TB
I'd say it was because you're declaring m as 6 indices long which, when the amount of characters put into it is 6, does not account for the string terminating character ('\0'). Make it m[7].
Sep 24 '09 #2
Markus
6,050 Expert 4TB
By the way, you answer your own question in your first sentence!

"According to a reference i am using, ANSI C does not have a built in string type. instead an array of characters need to be used. According to the book, the compiler ends then the array of char with the character zero '\o'. hence if you want to use a string of 4 chars, you need to declare an array of 5 chars."
Sep 24 '09 #3
Banfa
9,065 Expert Mod 8TB
You are confusing string length and array length.

The str* functions work on strings, that is they look for the character '\0' and will happily go out of bounds in an array to find it.

So when you wrote m[i]='\0' for any i<6 you altered the data and that altered the return value of strlen which operates on the data. If you had checked sizeof(m) you would have found it always to be the same unless you changed your declaration of m.
Sep 24 '09 #4
donbock
2,426 Expert 2GB
FYI
@elvira23
By the way, by declaring m as an array there is no doubt -- you can overwrite portions of the string. However, if you had done this ...
Expand|Select|Wrap|Line Numbers
  1. char *p = "elvira";
The compiler would have allocated a "hidden" char array, initialized it to "elvira", and then initialized p to point to that array. In this case, p points to a string literal; whether you could overwrite it (such as, p[5]='\0';) would be implementation dependent. Attempting to do so might have crashed your program!
Sep 24 '09 #5
weaknessforcats
9,208 Expert Mod 8TB
Also keep in mind that C has no control on array bounds. That makes m[100] perfectly accessible even though the array was defined with only 6 elements.

This is due to the fact that the array mane is the address of element 0. So
m[100] is the address of m[0] + (100 x size of the element) and the compiler can accept this with no warning.

It is your responsibility to control that array.
Sep 25 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

27
by: djake | last post by:
In the stroustrup C++ programming language (third edition) i can find example like this: string s1= "Hello"; So I imagine string is a standard class. Furthermore the class in the example is...
6
by: Chris Simmons | last post by:
I know that a String is immutable, but I don't understand why this piece of code fails in nUnit: // BEGIN CODE using System; class Test { public static void Main( String args )
27
by: fuch6921 | last post by:
I want to read in an Octal number argument and have it stored as an octal number. For instance the user will type: ./a.out 777 and it will store the octal number 777. But it atoi does this as an...
1
by: Richard Lewis Haggard | last post by:
I'm having a problem with what appears to be some sort of confusion with references. I have a single solution with a dozen projects which has been working quite nicely for a while. The references...
39
by: sucaba.r | last post by:
I don't know if this is a unique problem, or I'm going about it the wrong way. I currently connect to one of our SQL servers via a priviliged account (by using RUNAS). Works with no problem. I...
15
by: Lighter | last post by:
I find a BIG bug of VS 2005 about string class! #include <iostream> #include <string> using namespace std; string GetStr() { return string("Hello");
14
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
29
by: Andrea | last post by:
I want to write a program that: char * strplit(char* str1, char *str2, char * stroriginal,int split_point) that take stroriginal and split in the split_point element of the string the string...
11
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove...
14
by: spreadbetting | last post by:
I'm trying to split a string into an separate arrays but the data is only delimited by a comma. The actual data is one long string but the info is in a regular format and repeats after every five...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.