1

I have read that storing a string in a character array(with null termination) allows the string to be manipulated later on (unlike having a pointer to string literal).

#include <stdio.h>
int main()
{
   char s[10]="Stack";
   s[9]='a'; // a gets stored in array and if index is less than 6 string gets changed
   printf("%s\n",s);    
   return 0; 
}

Output : Stack

This works as long as index to be manipulated is less than length of string.

That means the string contents (and hence size) can't be changed even if there is empty space?

Is there any direct way(not using functions) to add 'a' at the position desired?

9
  • 1
    Can you explain more what is you want to do? Commented Sep 8, 2013 at 7:07
  • the short answer is "no" Commented Sep 8, 2013 at 7:07
  • 1
    I have no idea what you are asking. Are you aware of the fact that the 0-terminator makes all functions assume that there are no more characters in the string? Commented Sep 8, 2013 at 7:15
  • so there is no benefit of allocating an array here , the left space is lost and the string can't be manipulated Commented Sep 8, 2013 at 7:17
  • And if the OP is still around I give him this code for his amusement to show the dangers of writing into the last element of a character array: codepad.org/HDyACY5I Commented Sep 8, 2013 at 7:27

6 Answers 6

6

printf will only print the characters of a string before the NUL-terminator. When you set s[9]='a';, the contents of s become:

{'S', 't', 'a', 'c', 'k', '\0', '\0', '\0', '\0', 'a'}

if you print s[9], it's there:

printf("%c", s[9]);

Have a look at std::string.

Sign up to request clarification or add additional context in comments.

5 Comments

yes i checked that.so a possible way would be rewrite every null to whitespace and then use the last null to print the way one wants.
"memory may become like this" is there any other way also?
use std::string if you want to append chars to a string
thx. but please elaborate a bit on the memory representation.
@user2653718 When you initialize only part of an array or struct, like here a char array initialized with shorter string literal, then rest of allocated space is filled with 0. If you do not initialize it at all, then initial contents will be undefined.
3

You can always add and 'a' at the location, but the function used to print the string needs to be different. printf stops printing after it encounters a '\0' character. You can use a function like this

for (i = 0; i < len_of_str; i++)
{
    if (str[i] == '\0')
         continue;
    printf("%c", str[i]);
}

Comments

3

"That means the string contents (and hence size) can't be changed even if there is empty space?" You can certainly change hte string conent (aka individual characters) but you cannot change the storage size (number of elements, in this case number of chars) in the array. Since once you define the array (in this case, s), C allocates that much storage space to it and you cannot make any assumption about the memory outside that allocated space.

"Is there any direct way(not using functions) to add 'a' at the position desired?". Yes, directly assign it. The constraint is that you should not go beyond the storage space (excluding the NUL char). For example, you could easily do "s[2]='u';" and the output would be "Stuck" instead of "Stack".

1 Comment

yes that is true if i have index<length only that printf is able to read it. otherwise not.
2

after "stack" is set to s[10], s[0]='s', s[1]=[t]...s[4]='k', s[5]='\0'. In C and C++, when a char array is printed as a string format, the string's length is equal to the index of'\0', regardless of how much memory is allocated for the array.

Comments

1

the fast answer is "no" you can't change a size of an array of chars simply by doing

arr[size+1] = 'a';

as you can't change the size of array of integers the same way. but you can use a dynamic string with char * and allocate memory to is using malloc function

Comments

1

I changed your code a little bit and it's working fine.

   #include<stdio.h>
  using namespace std;
  int main()
  {
    char s[10]="Stack";
    s[8]='a'; // a gets stored in array and if index is less than 6 string gets changed
    s[5]='n';
    s[9]='\0';
    printf("%s\n",s);
    return 0;
 }

Output: stackn //'\0' characters are not printed are not printed First take out the null character '\0' present in s[5] position and replace with a character so that you can insert characters to the array s

Secondly, s[10] means 0-9 so replace your code s[9]='a' with s[8]='a' since to print the string you have to write null character at s[9]='\n'.

1 Comment

@user2653718: The answer uses C++, not C.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.