The moment you do bb = first;, bb and first are pointing to the same location of memory. first->a = 55; first->b = 55; first->c = 89; will change the values for a, b, and c in that location. The original value of first, is still lingering in memory but no way to access it anymore.

I think what you may want to do is *bb = *first;.

Answer from Anzurio on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/cpp_questions โ€บ does assigning a pointer to another pointer create a copy or does it just point to the same variable like a reference? if so whats the difference between using a pointer and a reference?
r/cpp_questions on Reddit: Does assigning a pointer to another pointer create a copy or does it just point to the same variable like a reference? If so whats the difference between using a pointer and a reference?
May 9, 2022 - Also read chapter 9 at https://www.learncpp.com/ (and all the other chapters too). ... A raw pointer behaves just like an int, as it has no other behaviour. So copying one has exactly the same behaviour as copying an int, except you are copying addresses not numbers (ok, an address is a number at the end of the day, but you are not allowed to treat it as a number).
Discussions

Copying pointers. - C++ Forum
Ok, Im not sure if Im going to explain myself properly but I will try mybest :P My question is: Can you copy a pointer? I mean, not copying the data, but copying the pointer so that you save another pointer pointing at the data. This might be a pretty inefficient way of doing what I want to ... More on cplusplus.com
๐ŸŒ cplusplus.com
c - When should I copy a pointer inside a function? - Software Engineering Stack Exchange
Stack Exchange network consists ... their careers. Visit Stack Exchange ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I'm trying to teach myself programming and I'd really appreciate some help with this issue. I've just read a tutorial on pointers but I have a problem with the example. The function copies one string ... More on softwareengineering.stackexchange.com
๐ŸŒ softwareengineering.stackexchange.com
Copy pointer to pointer in function - C - C++ Forum
Hello, I'd like to copy pointer to pointer in function and I don't want to use the library. But in main I have the old string instead a new string. Thanks ... Line 10 is just overwriting Str1, which is a local variable. If you need to change a pointer in main, you need two levels of indirection ... More on cplusplus.com
๐ŸŒ cplusplus.com
January 28, 2021
c - Copying data in pointers - Stack Overflow
What if I just want to point to the GPSLocation when creating another thread after doing the copy mentioned above? ... For just pointing to same memory use GPSLocation *destination = (GPSLocation *) ptr;. But be careful, because if you modify this memory via the pointer, the original one will ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
November 20, 2011
๐ŸŒ
Quora
quora.com โ€บ How-do-you-copy-a-pointer-to-another-pointer-in-the-programming-language-C
How to copy a pointer to another pointer in the programming language C++ - Quora
Answer: There are two ways thus can be done: 1. Do a shallow copy by assigning pointer A to pointer B 2. Do a deep copy by creating a new object by doing a deep copy of pointer Aโ€™s object into a new object K. The address of the newly created ...
๐ŸŒ
HowStuffWorks
computer.howstuffworks.com โ€บ tech โ€บ computer software โ€บ programming
Pointers: Pointing to the Same Address - The Basics of C Programming | HowStuffWorks
March 8, 2023 - Note that in this code, r points to the same thing that p points to, which is i. You can assign pointers to one another, and the address is copied from the right-hand side to the left-hand side during the assignment.
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 9846
Copying pointers. - C++ Forum
Warnis is right, pointers store values just like any other variable, but they store a special value, the adress of an object.But you can still work with their value just like any other variable. ... when you say array2[1] = array1[1];, it means 'the second pointer variable in array2 should point to the address stored in the second pointer variable of array1. (remember arrays start at 0). If you want to copy array1 completely to array2, you'll need to copy each element one by one, of course using a loop structure.
๐ŸŒ
YouTube
youtube.com โ€บ sanjay gupta tech school
Copy a string into another using pointer in c programming | by Sanjay Gupta - YouTube
Find Here: Links of All C language Video's Playlists/Video SeriesC Interview Questions & Answers | Video Serieshttps://www.youtube.com/watch?v=UlnSqMLX1tY&li...
Published ย  March 6, 2018
Views ย  14K
Find elsewhere
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 275738
Copy pointer to pointer in function - C - C++ Forum
January 28, 2021 - Hello, I'd like to copy pointer to pointer in function and I don't want to use the library. But in main I have the old string instead a new string. Thanks ... Line 10 is just overwriting Str1, which is a local variable. If you need to change a pointer in main, you need two levels of indirection ...
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 124584-copying-structures-containing-pointers.html
Copying structures containing pointers
March 10, 2010 - obj2=obj1; (*obj2.x1)++; printf("obj2 ... only normally. But if you want to new memory you need to allocate the new memory. ... I guess that if I use the memcpy function, I will get a copy of the structure pointer only and not the whole allocated area via this pointer...
๐ŸŒ
W3Schools
w3schools.in โ€บ c-programming โ€บ examples โ€บ copy-string-using-pointers
C Program to Copy String Using Pointers - W3Schools
C program to Copy string without using strcmp() function by creating our own function which uses pointers. ... #include<stdio.h> void copy_string(char*, char*); main() { char source[100], target[100]; printf("Enter source string\n"); gets(source); copy_string(target, source); printf("Target string is \"%s\"\n", target); return 0; } void copy_string(char *target, char *source) { while(*source) { *target = *source; source++; target++; } *target = '\0'; } ... This program is written to demonstrate how to copy a string using pointers.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ c-copying-data-using-the-memcpy-function-in-c
C: Copying data using the memcpy() function in C
In C, the memcpy() function copies n number of characters from one block of memory to another. ... Destination: Pointer to the destination array where data will be copied.
๐ŸŒ
Bytes
bytes.com โ€บ home โ€บ forum โ€บ topic
Get copy of object from pointer? - C / C++
November 1, 2015 - Call c2 = *p; c2 is created as a copy of the object pointed to by p. Gavin Deane ... Alf P. Steinbach ... Re: Get copy of object from pointer? * Mike:[color=blue] > I have a class "Call". I have instantiated a Call object in one class then > passed a pointer to that object to another class.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-write-a-C-program-that-uses-pointers-to-copy-an-array-of-integers
How to write a C program that uses pointers to copy an array of integers - Quora
Answer (1 of 4): Iโ€™m not going to give you the answer, but I will explain some concepts. You may be confused by how C does array access. You cannot copy an array by trying to do an assignment. Say you have two integer arrays, a and b. You cannot copy a into b by doing: [code]b = a; [/code]In fa...
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2545188 โ€บ how-can-i-do-a-copy-of-a-pointers-list-c
How can I do a copy of a pointers list? C++
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
๐ŸŒ
Tutorials
onlinetutorialhub.com โ€บ home โ€บ c language โ€บ copy string using pointers in c programming language
Copy String Using Pointers In C Programming Language
May 26, 2025 - Pointer arithmetic (moving the pointer to the next character) is done with the ++ operator, whereas dereferencing (accessing the character at the current pointer location) is done with the * operator. The loop keeps going until the null character \0 is copied, which indicates that the copied ...
Top answer
1 of 1
5

Is there something wrong with it?

Yes, there is. In each of these cases there is a memory leak. In addition, some of these snippets may do the wrong thing (i.e. the program gives a wrong answer in addition to leaking memory, or just crashes).

In order to understand the memory leak, let's review what assignment does in C. In the words of the standard:

In simple assignment (=), the value of the right operand [...] replaces the value stored in the object designated by the left operand.

In simple down-to-earth words, the left operand forgets the value it was holding before, and starts holding something else.

Now let's recall that a pointer is a value that points to some data (an object, or a block of memory).

What are these values that are copied and forgotten in these statements?

p = malloc(sizeof(something));

// p stores the value returned by malloc, which is a pointer to 
// a block of memory. note that this value is the only pointer in 
// existence that points to that block of memory.

p = something_else;

// p forgets that it was storing a pointer to a block of memory 
// returned by malloc, and now stores a different pointer,
// presumably pointing to another block of memory. 
// the old pointer is now lost forever. there is no other pointer
// that points to the same block.

This is the very definition of memory leak.

Note that the block of memory itself is not touched or modified in any way, shape, or form. Only pointers are changed.

It's the confusion between the pointer and the pointed-to data that leads to this error. It seems that people commit it operate under a false impression that p = malloc(...) allocates space for data, and p = something_else assigns the data to space just allocated. This is indeed not how pointers work. Both statements only assign pointers, and do not touch pointed-to data.

What would be the right way to do this?

It depends on what is "this", i.e. on what you need. There is no universal recipe. In some cases you just want to copy a pointer. Drop the malloc line, and use the second assignment directly:

p = something_else; // no malloc here, just pointer assignment

In other cases you need to copy data pointed to by something_else into a new block of memory returned by malloc and now pointed by p (as opposed to something_else itself, which is a pointer):

p = malloc(sizeof(*p));           // note how sizeof is used here.
copy_data (p, something_else);    // always prefer this over sizeof(some_type)

Of course there is no such thing as copy_data in C. This is something you need to provide yourself, or use one of the standard functions. For example, if you need to copy a string:

p = malloc(strlen(something_else) + 1); // note no sizeof here. the size is dynamic
strcpy(p, something_else);              // also note +1 for the null terminator

// these two lines can be replaced by p = strdup(something_else);
// strdup is a function that has recently entered the C standard, but it was
// supported by all major compilers since the dawn of time

If you need to shallow-copy a struct:

p = malloc(sizeof(*p));
memcpy(p, something_else, sizeof(*p));
// or, if both p and something_else are declared 
// as pointers to struct something
*p = *something_else;

// if the struct contains pointers, this is probably not what you want
๐ŸŒ
ProCoding
procoding.org โ€บ c-program-to-copy-one-array-to-another-using-pointers
C program to copy one array to another using pointers | ProCoding
March 9, 2024 - Arrays: Arrays in C are collections of elements of the same data type stored in contiguous memory locations. They offer a convenient way to store and manipulate a fixed-size sequence of elements. Pointers: Pointers are variables that store memory addresses. They allow direct access to the memory locations where data is stored. Pointers play a vital role in array manipulation, enabling efficient memory management and dynamic allocation. To copy one array to another in C, we leverage pointers to iterate through the elements of both arrays and perform the copy operation.