Thread: problem with array of strings

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    12

    problem with array of strings

    hello


    i have string array of variable size. could anyone let me know how to make sure that at the null terminater '\0' is at the end of the string

    Code:
    
    here is code :#include <stdio.h>
    #include <math.h>
    
    #define  SIZE  9
    
    int main()
    	{
    
    	char Buff  [ SIZE ] ;
    	int  result;
    
    	result =SIZE + 1 ;
    	Buff[result] = '\0';
    	Buff[result/2] = '|';
    
    	printf("%c\n\n\n",Buff[result]);
    	
    	return 0;
    
    	}
    when i remove the line Buff[result] = '\0' i donot get any error during the execution of porgram. but with that line include i get the following error message

    runtime error #2 the stack around the variable Buff was corrupted

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    It is Buff[SIZE-1] = '\0', since you have SIZE elements from 0 to SIZE-1

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    'result' isn't a valid index. I'm not sure what you meant to do but you could possibly do this:

    #include <cstring>
    #define SIZE 9

    char buff[SIZE + 1];
    int i = 0;
    buff[i++] = '|';
    buff[i] = 0;

    Of course since this is c++ another option is to use the string class, which will manage some of the more tedious things for you.

    #include <string>

    std::string buff;
    buff.push_back('|');

    Good luck.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    12
    i used "for" to fill the blanks in string array with " " (space) , I still get the error message.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quite simply put, you're writing beyond the end of the array.
    If you do...
    char buff[SIZE]
    ...then the indexes range from 0 to SIZE - 1.
    SIZE is out of bounds!

    And if that doesn't help, you need to show the code you used.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    I think this is what you're after.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define  SIZE  9
    
    int main()
    	{
    
    	char Buff[SIZE] ; // Declaring an array size of 9 elements 0-8.
    	int result;
    
    	result = SIZE - 1; // result = 8
    	Buff[result] = '\0'; // Buff[8] = null terminator.
    	Buff[result/2] = '|'; // Buff[4] will now hold the character '|'.
    
    	printf("&#37;c\n\n\n",Buff[result]); // Are you sure you want to print the null terminator?
    	
    	return 0;
    
    }
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    12
    thank you guys for replying.

    the way i understnd is that if the char array is declared as follow

    #define SIZE 10

    char buff [SIZE]

    then i can only write in elelments 0 to 8
    and element 9 is automatically filled with '\0'
    and that u can't write to element 9 even if it is '\0';

    if i am correct just let me know.

    although '\0' is automatically placed , now another question is how i ensure that '\0' is at the end of array without writing '\0' at the end of array.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cole View Post
    thank you guys for replying.

    the way i understnd is that if the char array is declared as follow

    #define SIZE 10

    char buff [SIZE]

    then i can only write in elelments 0 to 8
    Everything up to here is fine.
    and element 9 is automatically filled with '\0'
    and that u can't write to element 9 even if it is '\0';

    if i am correct just let me know.

    although '\0' is automatically placed , now another question is how i ensure that '\0' is at the end of array without writing '\0' at the end of array.
    And all of this is just wrong. Nothing is automatically written into a char array for you, and therefore you must do buff[9] = '\0' yourself.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    12
    just to clarify , i declare a character array as follow

    char buff[] = "string";


    would not the character array will have s, t , r , i ,n ,g , \0 ;

    that means total length of 7 elements and

    \0 would be placed automatically because you declared a array of type char,
    if the above statement is wrong, then how do i place \0 myself. As i said i tried to write \0 at element 7 and i get error message (the one i posted at the very begining of thread)

    thanking you

    c

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If that is really what you did, then you are correct, '\0' is indeed at buff[6].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by cole View Post
    just to clarify , i declare a character array as follow

    char buff[] = "string";


    would not the character array will have s, t , r , i ,n ,g , \0 ;

    that means total length of 7 elements and

    \0 would be placed automatically because you declared a array of type char,
    if the above statement is wrong, then how do i place \0 myself. As i said i tried to write \0 at element 7 and i get error message (the one i posted at the very begining of thread)

    thanking you

    c
    It is not correct actually. It is not because you declared an array of char that the last element is \0. Is because you initialized it with a string literal. A string literal ("something") includes the \0, even though you don't "see" it. If you write this:"a" , anywhere you write this, the compiler will see an array of 2 characters, one 'a' and one '\0'. In whatever function that lets you use strings or char* if you put a string literal then that literal includes the \0.
    But, a char of array does NOT include \0 unless you put it somehow.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    When you use a string literal (something enclosed in double-quotes) then you are implicitly saying that you want a null char at the end. I.e. You asked for the string "string" only, and not the string "string plus some other random crap that happens to follow".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  2. array of strings problem
    By dayknight in forum C Programming
    Replies: 3
    Last Post: 11-08-2005, 10:41 AM
  3. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 05:03 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. multidimentional array of strings ???
    By null in forum C Programming
    Replies: 2
    Last Post: 09-26-2001, 11:20 PM