简体   繁体   中英

Pointers of strings in C

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;
}

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!

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 )

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;
}

No. out is a different variable that holds the same address str is at, ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM