Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
1  
Can you explain more what is you want to do? –  pablo1977 Sep 8 at 7:07
 
the short answer is "no" –  No Idea For Name Sep 8 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? –  H2CO3 Sep 8 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 –  user2653718 Sep 8 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 –  dcaswell Sep 8 at 7:27
show 4 more comments

6 Answers

up vote 6 down vote accepted

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.

share|improve this answer
 
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. –  user2653718 Sep 8 at 7:18
 
"memory may become like this" is there any other way also? –  user2653718 Sep 8 at 7:40
 
use std::string if you want to append chars to a string –  billz Sep 8 at 7:49
 
thx. but please elaborate a bit on the memory representation. –  user2653718 Sep 8 at 8:00
 
read this link, it will be helpful en.cppreference.com/w/c/io/fprintf –  billz Sep 8 at 8:03
show 1 more comment

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]);
}
share|improve this answer
 
yes thx this way i can read it. –  user2653718 Sep 8 at 7:59
add comment

"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".

share|improve this answer
1  
yes that is true if i have index<length only that printf is able to read it. otherwise not. –  user2653718 Sep 8 at 7:39
add comment

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.

share|improve this answer
add comment

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'.

share|improve this answer
 
no use of namespace...compilation error. –  user2653718 Sep 8 at 7:44
 
@user2653718: The answer uses C++, not C. –  alk Sep 8 at 7:48
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.