Reputation: 39
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?
Upvotes: 2
Views: 158
Reputation: 23218
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);
Upvotes: 1
Reputation: 63
#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.
Upvotes: 1
Reputation: 63
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))
Upvotes: 1
Reputation: 372814
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."
Upvotes: 6