2

I wrote this code

#include <stdio.h>

int main()
{
    char String[] = "Hello the world!";
    char *pointer = String;
    int i;

    printf(" %s\n", pointer);

    pointer = "Helllooooo the worlldddddd";
    printf("Hello %s\n", pointer);
    printf("Hello %s\n", String);

    return 0;
}

but I can't understand how this line works fine.

pointer = "Helllooooo the worlldddddd";

but I got this output

 Hello the world!
Hello Helllooooo the worlldddddd
Hello Hello the world!

As you see it couldn't change String value but it shows more than the original number of characters. Shouldn't this cause a buffer overflow? Won't that destroy other variables?

1
  • 1
    "Hello the world!" and "Helllooooo the worlldddddd" are two different strings, located in two different parts of memory. With pointer = ..., you are setting pointer to be the address of either one of these strings (i.e., setting it to point to that string). Commented Feb 22, 2017 at 18:59

4 Answers 4

6

When you write the line

pointer="Helllooooo the worlldddddd";

you are not saying "take the array pointed at by pointer and overwrite its contents with the string "Helllooooo the worlldddddd"," but rather "change which string pointer points at so that it's now pointing at the string ""Helllooooo the worlldddddd"." This accounts for why you're seeing the original string printed out when you directly print String - you never actually modified it. As a result, you don't need to worry about overflowing your array here, since you're not actually writing anything to it.

On the other hand, if you wrote

strcpy(pointer, "Helllooooo the worlldddddd");

which actually does copy the contents of the new string into the buffer pointed at by pointer, then you would have a buffer overflow, which would be a problem. But notice that this is a very different operation that explicitly says "please copy the contents of this string to this location" rather than "change where this pointer points."

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

2 Comments

@JohnH - I do not believe String is considered const, implicit or otherwise, so although more space cannot be added to String, for example with realloc(...), it can be modified. If it was implicit const, I would think it would throw a compile time error as opposed to a run-time error. Is there a specific environment where you have observed otherwise?
@JohnH, no String must be mutable. It is not pointing to the string literal but a completely separate array of char that is not const qualified. So per se there is no problem modifying it. (There is obviously a problem with the size, though.)
1

you have initialize a pointer which point to String "Hello the world " char *pointer=String; so far so good .
first printf printf(" %s\n",pointer);, you have printed the pointer which point to "Hello the world".
then you set the pointer to point anew string "Hellloooooo the worllddddd" pointer="Helllooooo the worlldddddd";.
then you have printed the pointer which point in this case to "Hellloooooo the worllddddd"printf("Hello %s\n",pointer); .
last printf you have printed the string "Hello the world" printf("Hello %s\n",String);.
Note:. ((takecare that you have printed in second printf("Hello %s\n",String); and third printf printf("Hello %s\n",String); the string hello which will be printed before the value of the pointer or the string))

Comments

1
#include <stdio.h>

int main()
{
char String[]="Hello the world";  // the string
char *pointer=String;             // pointer to string 
char i;                           //counter

printf(" %s\n",pointer);          // print current value of pointer

pointer="number of char";         // new value to replace the string
for (i=0;i<14;i++)                // you cannot change the content of array without using loop
{
String[i] = pointer[i];           // char i in string = ti char i in pointer
}
printf("Hello   %s\n",pointer);   // print value of pointer
printf("Hello   %s\n",String);    // print value of string

return 0;
} 

i think that what are you trying to do.

Comments

1

One more quick option for you to consider when manipulating strings:
The string function strdup(...); allows the copying of an existing string into a newly created string in one easy step. (or at least one in which all the complexity has been abstracted away):

 char *pointer = strdup("Helllooooo the worlldddddd");

The new string pointer can now be used to contain any string up to len long, where len is defined as:

int len = strlen(pointer);//len is 26 after using strdup(...); above

So for example, because your example string is shorter than len:

char String[]="Hello the world!";//length is 16

you could copy it into pointer without a buffer overflow:

strcpy(pointer, String);

Strings created using strdup(...): need to be free'd to avoid memory leaks. Call the following when you are finished using pointer:

free(pointer);

Comments

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.