0

I'm currently playing around with passing char array to a char pointer. Lots of the examples I see show how you need to allocate the memory that the string pointer will use before copying the char array to it. And when copying, you iterate through the array to store each address into the allocated char pointer.

In the example below I don't initialize the char pointer and neither do iterate through the array. I just pass the pointer of the first element.

int main()
{
    char c[10] = "something";
    // char* new_c = (char *)malloc(strlen(c)+1);
    char *new_c = NULL;
    new_c = c;
    printf("%s", new_c);

    return 0;
}

Why is new_c still printing the entire string? Why do people even bother iterating through the entire array to copy?

9
  • Try to read two strings from the user and print them after reading both of those strings. Then you will understand. Commented Dec 15, 2018 at 8:56
  • it's not copying, it's just taking the same start for the string through pointers Commented Dec 15, 2018 at 8:56
  • related: stackoverflow.com/questions/32547413/… Commented Dec 15, 2018 at 8:57
  • Possible duplicate of Stack pointer difference for char pointer and array Commented Dec 15, 2018 at 8:58
  • 1
    "I don't initialize the char pointer". You do, with new_c = c;. The array c decays to a pointer, just as if you passed c directly to printf. And then it is printf which iterates through the array until the nul terminator is found. Commented Dec 15, 2018 at 9:08

3 Answers 3

3

Run this program and you will get a clear picture of what is happening

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

int main(void) {
    char c[10] = "something";
    char *new_c = NULL;
    char new_c_2[10] = "";

    new_c = c; // copies address of 'c' to 'new_c' 


    for(int i=0; c[i]!='\0'; i++) {
        new_c_2[i] = c[i]; // copies value of 'c' to 'new_c_2'
    }

    // Data before changing the value of 'c'
    printf("\nData before changing the value of \'c\'\n");
    printf("new_c = %s\n", new_c);
    printf("new_c_2 = %s\n", new_c_2);

    strcpy(c, "changed");

    // Data after changing the value of 'c'
    printf("\nData after changing the value of \'c\'\n");
    printf("new_c = %s\n", new_c);
    printf("new_c_2 = %s\n", new_c_2);

    return 0;
}

OUTPUT:

Data before changing the value of 'c'
new_c = something
new_c_2 = something

Data after changing the value of 'c'
new_c = changed
new_c_2 = something

char *new_c = NULL; new_c = c;

These statements are just pointing 'new_c' to the address of 'c'. So, if you change the value of 'c', and use 'new_c' it will go to the address of 'c' and give the updated value.

We copy the string into another so that we can use the old value even if we change the value of 'c'.

Please refer to call by value and call by reference in C programming for further details.

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

Comments

1

You say "I don't initialize the char pointer". You do, with

new_c = c;

The array c decays to a pointer, just as if you had passed c directly to printf. And then it is printf which iterates through the array until the zero terminator is found.

Comments

0

Why do people even bother iterating through the entire array to copy?

well, maybe to be sure the original reference isn't deleted/overwritten after a while.

For instance, if the original buffer is a local/automatic variable, which will be invalid after returning from the function.

Or if I just do c[0] = '\0';. new_c doesn't display a thing now because I just killed the only copy of the string.

Both copy & referencing another string through a "shallow" pointer are useful, depending on the use cases.

3 Comments

The language of the answer is too complicated for a beginner.
@perreal but explaining in layman terms is too board for SO. 1) We need to tell what is pointer. 2) What is array. 3) Difference between array and pointer. list goes on.....
That makes sense. The code I provide was making a shallow copy, Where as iterating through the char array and copying its values to another array/memory location thus making the string persistent. Thanks!

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.