Hi,

i dont understand the following; PLZ fix me.

when i try to change the character pointed by pointer the behaviour is undefined.

but when i change the same by using pointer as formal parameter of function it will be ok.

why i cant change the base address of array, when i can change the pointer value.

int main()
{
	char *ptr = "String";
	char arr[]= "Array";
	*str ='T'; // behaviour is undefined
	arr = ptr; // not allowed
	modify(arr);
	return 0;
}

int modify(char *p)
{
	*p = 'X';// no problem
}

the second problem is

2.

int main()
{
     printf("Hello" "World"); // HelloWorld
     printf("%s%s","Hello" "World"); // HelloWorld(someGarbage)
     return 0;
}

how first printf printing o/p: HelloWorkld ( why concatenating).
The printf should stop priting whenever it finds first '\0'.
and the second printf printing some garbages after the strings.

Thanks,
Danian.

The first example your trying to change a const string - char *ptr = "String";
with the line *str ='T';(Note I assumed str is a typo and should be ptr) which is not allowed

and for the "Hello, World" try:

printf("%s%s","Hello", "World");

OK. One step at a time.

char *ptr = "String";
	char arr[]= "Array";
	*ptr ='T'; // behaviour is undefined

ptr points to a string literal, which resides in a read-only section of your data. An attempt to modify it results in segfault, GPF or something else depending of the platform (it may even succeed if a memory protection is not implemented)

arr, however, is allocated on a stack and is just initialized from the literal. This memory is perfectly writable.

arr = ptr; // not allowed

Arrays are not pointers. The are "same" only as function arguments.

printf("Hello" "World"); // HelloWorld

The preprocessor does concatenate strings, that is right.

printf("%s%s","Hello" "World"); // HelloWorld(someGarbage)

You pass one parameter (the concatenated string) and ask to print two.

you put space on %s%s
and assign to 2 variables or text.

printf("%s %s","Hello", "World");

*str ='T'; // behaviour is undefined
because there is no variable type in front.

char *str = "T";

OK. One step at a time.


ptr points to a string literal, which resides in a read-only section of your data. An attempt to modify it results in segfault, GPF or something else depending of the platform (it may even succeed if a memory protection is not implemented)

arr, however, is allocated on a stack and is just initialized from the literal. This memory is perfectly writable.

Arrays are not pointers. The are "same" only as function arguments.


The preprocessor does concatenate strings, that is right.


You pass one parameter (the concatenated string) and ask to print two.

yes you are right.

but still i dont understand the second one.
printf("%s%s","hello""world");
how that is getting concatenated

as it getting garbages because there is no value for second %s.

yes you are right.

but still i dont understand the second one.
printf("%s%s","hello""world");
how that is getting concatenated

as it getting garbages because there is no value for second %s.

You have two strings with nothing between them. The preprocessor combines them together. It is a documented language feature. "aaa" "bbb" is the same as "aaabbb" .

Hi,

I guess answering your question requires some knowledge of how memory is allocated for constants and variables. Now let me answer your question one by one .

1.

char *ptr = "String";
char arr[]= "Array";
*str ='T'; // behaviour is undefined

Now this surely is not allowed for the simple reason that you can't modify pointers who are pointing to string literals and it is this because string literals are stored in read only part of the memory and any attempt to modify them will result in segmentation fault. And since str is a pointer pointing to a string literal(which i have assumed as you haven't mentioned) and therefore modifying it is not allowed.

2.

arr = ptr; // not allowed

This is not allowed because arrays are not pointers. Arrays are automatically allocated space, but can't be relocated or resized where as pointers must be explicitly assigned to point to allocated space (using malloc), but can be reassigned (i.e. pointed at different objects).

3.

int modify(char *p)
{
*p = 'X';// no problem
}

The reason that this segment is executing without any problem is because what you are passing in (pointer)p is the address of an array(arr[]). And since arrays are allocated memory on the stack and therefore the contents of this memory can be modified using pointers. In fact, you can modify any array using pointers and this is what pointers are best at doing.

4.

int main()
      {
      printf("Hello" "World"); // HelloWorld
      printf("%s%s","Hello" "World"); // HelloWorld(someGarbage)
      return 0;
      }

Now this is really silly as you are writing '%s' two times in printf() but you are not providing the two variables it need to print those two strings. The right way of doing this would be :
printf("%s %s","Hello","World");
You have to provide comma between hello and world for printf to interpret it right.

Hi,

I guess answering your question requires some knowledge of how memory is allocated for constants and variables. Now let me answer your question one by one .

1.

char *ptr = "String";
char arr[]= "Array";
*str ='T'; // behaviour is undefined

Now this surely is not allowed for the simple reason that you can't modify pointers who are pointing to string literals and it is this because string literals are stored in read only part of the memory and any attempt to modify them will result in segmentation fault. And since str is a pointer pointing to a string literal(which i have assumed as you haven't mentioned) and therefore modifying it is not allowed.

2.

arr = ptr; // not allowed

This is not allowed because arrays are not pointers. Arrays are automatically allocated space, but can't be relocated or resized where as pointers must be explicitly assigned to point to allocated space (using malloc), but can be reassigned (i.e. pointed at different objects).

3.

int modify(char *p)
{
*p = 'X';// no problem
}

The reason that this segment is executing without any problem is because what you are passing in (pointer)p is the address of an array(arr[]). And since arrays are allocated memory on the stack and therefore the contents of this memory can be modified using pointers. In fact, you can modify any array using pointers and this is what pointers are best at doing.

4.

int main()
      {
      printf("Hello" "World"); // HelloWorld
      printf("%s%s","Hello" "World"); // HelloWorld(someGarbage)
      return 0;
      }

Now this is really silly as you are writing '%s' two times in printf() but you are not providing the two variables it need to print those two strings. The right way of doing this would be :
printf("%s %s","Hello","World");
You have to provide comma between hello and world for printf to interpret it right.

Thanks for all your answers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.