-3

In the case down below. Does changing the string 'out' change the string 'str' respectively? In other words, do they have the same pointer?

Thank you in advance.

int main() {
  char str[]={'g','o','o','d','/0'};
  char special[]={'o','/0'};
  char* out=str;
  return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Sam12
  • 1,805
  • 2
  • 15
  • 23

4 Answers4

5

It depends. If you write:

out = "hello!";

you do not change the string str, but simply make out point to another memory location.

But if you write into out like in this:

sprintf(out, "abcd");

then you do change str. But beware of overflow!

user1738687
  • 457
  • 3
  • 12
5

For starters I think you mean the terminating zero '\0' instead of the multibyte character literal '/0'.

To escape such an error it is better to initialize character arrays with string literals (if you are going to store a string in an array). For example

char str[] = { "good" };

or just like

char str[] = "good";

As for the question then after this assignment

char* out=str;

the pointer out points to the first character of the array str. Thus using this pointer and the pointer arithmetic you can change the array. For example

char str[] = "good";
char *out = str;

out[0] = 'G';
*( out + 3 ) = 'D';

puts( str );          

Moreover an array passed as an argument to a function is implicitly converted to pointer to its first character. So you can use interchangeably either an array itself as an argument or a pointer that initialized by the array designator. For example

#include <stdio.h>
#include <string.h>

//...

char str[] = "good";
char *out = str;

size_t n1 = strlen( str );
size_t n2 = strlen( out ); 

printf( "n1 == n2 is %s\n", n1 == n2 ? "true" : "false" );

The output of this code snippet is true.

However there is one important difference. The size of the array is the number of bytes allocated to all its elements while the size of the pointer usually either equal to 4 or 8 based on used system and does not depend on the number of elements in the array. That is

sizeof( str ) is equal to 5
sizeof( out ) is equal to 4 or 8

Take into account that according to the C Standard the function main without parameters shall be declared like

int main( void )
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

I think there's a typo in your code, you have written '/0' but it's not a null character but '\0' is. As far as out & str are concerned, str[] is a char array, whereas out is a pointer to it. If you make out point to some other char array there'll be no effect on str. But you can use out pointer to change the values inside the str[], like this,

int main( void )
{
  char str[]={'g','o','o','d','\0'}; // There was a typo, you wrote '/0', I guess you meant '\0'
  //char special[]={'o','\0'};
  char* out=str;
  for(int i=0; out[i] != '\0'; i++)
  {
     out[i] = 'a';
     // This will write 'a' to the str[] 
  }
  printf("out: %s\n", out);
  printf("str: %s", str);
  return 0;
}
WhiteSword
  • 101
  • 9
0

No. out is a different variable that holds the same address str is at, i.e. it points to the same location. Note that changing *str will change *out.

In C, assignment takes the value of the right end and stores it in the left end, it does not makes the right "become" left

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
  • `str` doesn't hold an address. It's an array. – StoryTeller - Unslander Monica Jul 11 '17 at 13:52
  • @StoryTeller What's the difference in C? – klutt Jul 11 '17 at 13:53
  • 1
    @klutt - [You won't find a better explanation than this](https://stackoverflow.com/questions/4607128/in-c-are-arrays-pointers-or-used-as-pointers) – StoryTeller - Unslander Monica Jul 11 '17 at 13:54
  • @StoryTeller I used that since str == &str. I tried to correct my answer – CIsForCookies Jul 11 '17 at 13:55
  • 3
    @CIsForCookies - `str == &str` should raise several compiler warnings about comparing incompatible pointer types. The fact `str` decays to the address of its first byte when used in an expression, doesn't make it a pointer. – StoryTeller - Unslander Monica Jul 11 '17 at 13:56
  • @StoryTeller Indeed, but the values are the same, so what I was thinking is that I can look at 'str' as an array and as a pointer to the first element and thus I can say str holds an address. I'm just rationalizing my mistake... clearly it was wrong :) – CIsForCookies Jul 11 '17 at 14:00
  • @ClsForCookies a *pointer* is a stored object itself, so you have some location containing the value of the pointer. As a consequence, you can for example **assign a different value** to a pointer. The stored object of the array is just the sequence of its elements. Evaluating the identifier of the array results in a pointer to its first element, but that doesn't make the array a pointer. You can't assign to it, for example. –  Jul 11 '17 at 14:03