Assignment of structures with pointers in C - c

Can anyone explain the output of the following piece of code:
int main() {
struct student {
char *nume;
char an[5];
} a, b;
a.nume = (char*)malloc(20);
strcpy(a.nume, "Alex Popescu");
strcpy(a.an, "1996");
printf("%s %s\n", a.nume, a.an);
b = a;
strcpy(b.nume, "Emil Ionescu");
strcpy(b.an, "1997");
printf("%s %s\n", b.nume, b.an);
struct student *pa = &a;
printf("%s %s\n", pa->nume, (*pa).an);
free(a.nume);
return 0;
}
The output is:
Alex Popescu 1996
Emil Ionescu 1997
Emil Ionescu 1996
What does b = a; do? Is b pointing to the same address as a? And when you change b.name to Emil Ionescu, a is changeing it's value too?
Why is only the name changed but the year remains 1996?

It does a 'shallow' copy of the struct.
Every member in a is copied to every member in b. Since a pointer points to some memory, in this case both pointers of both structs will point to the same memory. If that memory is changed, then both pointers will see that change.
But the array contains it's own memory, so the change of the array in one struct, will only affect that struct.
I think you are confused because you used a struct. There is nothing special about it, it just happens to hold a pointer and an array. You code would be the same if instead of two structs you used two pointers and two arrays and copied them manually. You should try that.

Related

Access with a pointer on two structures with an array

I got another issue with my Code. I want to access on all 3 values with a simple pointer usage. But my Pointer jumps too far.
My Output is this:
Value is 1
Value is 4194432
Value is 2686824
I guess that my Problem is, that my pointer is from the wrong type. So pointer++ dont jump to the next array element. I really dont know, how i can solve this.
(It's only a simple example to reproduce my problem, so dont look for sense in doing this)
typedef struct _A {
int value;
}A;
typedef struct _B {
A array[10];
}B;
int main()
{
A atest;
B btest;
B *p=NULL;
btest.array[0].value=1;
btest.array[1].value=3;
btest.array[2].value=5;
p=malloc(10 * sizeof(btest));
p=&btest.array;
int i=0;
for(i=0;i<3;i++)
{
printf("\n%d. Value is %d\n",i+1,p++->array->value);
}
return 0;
}
Thx for reading and if something is missing in my explanation, then i will apologize and add it.
EDIT:
Additionally to Some Programmers Dude Sollution, i add a simple cast mechanic:
q=(A*) p;
q++;
p =(B*)q;
Thx for your help :)
These lines
p=malloc(10 * sizeof(btest));
p=&btest.array;
contains two problems. The first is that with the second assignment you lose the memory you allocated. It does not copy the contents of btest.array.
The second problem is that p is a pointer to B but &btest.array is a pointer to an array of ten A elements (its type is A (*)[10]). Those two are very different types.
If you want to copy btest into the memory allocated for p (which is a pointer to ten B structures which is a little overkill) do e.g.
p=malloc(sizeof *p);
memcpy(p, &btest, sizeof *p);
Then to the next problem:
p++->array->value
The p++ will make p point to the next element in the array pointed to by p, but that's wrong. You should loop over p->array instead like
p->array[i].value

Copy one pointer content to another

I thought I've read somewhere that when using pointers and we want to copy the content of one to another that there are two options:
using memcpy or
just assigning them with = ?
However in the lower example, I just tested it by allocating memory for two pointers, then assigning the second, changing first..but then the entry of my second pointer is also changing.. What am I doing wrong :/.
typedef struct {
int a;
int b;
int c;
} my_struct;
int main(int argc, char** argv) {
my_struct* first = malloc(sizeof(my_struct));
first->a = 100; first->b = 101; first->c = 1000;
my_struct* bb = malloc(sizeof(my_struct));
printf("first %d %d %d\n", first->a, first->b, first->c);
bb = first;
printf("second %d %d %d\n", bb->a, first->b, bb->c);
first->a = 55; first->b = 55; first->c = 89;
printf("second %d %d %d\n", bb->a, first->b, bb->c);
}
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;.
Your knowledge about memcpy is correct but you cannot assign contents of "location pointed to by the pointers" just by assigning pointers like you did in your statement mentioned above.
You are assigning one pointer to the another in the following statement:
bb = first;
Now both these point to the same memory location (think of bb as an alias to first).
If you want to copy data then you copy using "data pointed to by pointers" *bb = *first
As has already been pointed out, if you have a pointer first that points to some location in memory, and you make the assignment bb = first, where bb is a compatible pointer type, then bb points to the same address as first. This does not copy the contents of the memory referenced by first to the location originally referenced by bb. It copies the value of the pointer, which is an address, to bb.
If you define an array A, you can't make the assignment B = A to copy the contents of A to B. You must use strcpy() or memcpy() or some such function. But structs are different. You can assign the contents of one struct to a compatible struct.
In your example, bb and first are pointers to structs, and when you write bb = first, now both pointers reference the same address in memory, and you no longer have access to the memory originally referenced by bb-- so now you have a memory leak! But *bb and *first are structs, and when you write *bb = *first, the contents of the struct *first are copied to the struct *bb. So now you have two different structs, at different locations in memory, each with copies of the same three ints.
If your my_struct type contained a pointer to int, then after the assignment *bb = *first they would each contain a copy of a pointer to the same location in memory, but the data referenced by those pointers would not be copied. So, if the structs contained a pointer to an array, only the pointer would be copied, not the contents of the array, which would be shared by the two structs.
you need to copy the data pointed by pointers like *p1 = *p2. But Remember, this will not work if you have pointers again inside the structures you are copying.

Behavior of struct copying in C

I have been reading a number of questions on this site about how to duplicate structs in C. I've been playing around with some code, trying to understand the differences between 'shallow' copying (where the new struct is simply assigned a pointer to the memory address of the first struct) and 'deep' copying (where the data is copied member-by-member into a new chunk of memory).
I created the following code, assuming that it would show the 'shallow' copying behavior:
#include <stdio.h>
struct tester
{
int blob;
int glob;
char* doob[10];
};
int main (void)
{
//initializing first structure
struct tester yoob;
yoob.blob = 1;
yoob.glob = 2;
*yoob.doob = "wenises";
//initializing second structure without filling members
struct tester newyoob;
newyoob = yoob;
//assumed this line would simply copy the address pointed to by 'yoob'
//printing values to show that they are the same initially
printf("Before modifying:\n");
printf("yoob blob: %i\n", yoob.blob);
printf("newyoob blob: %i\n", newyoob.blob);
//modifying 'blob' in second structure. Assumed this would be mirrored by first struct
newyoob.blob = 3;
//printing new int values
printf("\nAfter modifying:\n");
printf("yoob blob: %i\n", yoob.blob);
printf("newyoob blob: %i\n", newyoob.blob);
//printing memory addresses
printf("\nStruct memory addresses:\n");
printf("yoob address: %p\n", &yoob);
printf("newyoob address: %p\n", &newyoob);
}
Output on running:
Before modifying:
yoob blob: 1
newyoob blob: 1
After modifying:
yoob blob: 1
newyoob blob: 3
Struct memory addresses:
yoob address: 0x7fff3cd98d08
newyoob address: 0x7fff3cd98cb0
Is this code create a deep copy, as it appears, or am I misunderstanding what's happening here?
The shallow vs. deep copy problem is only relevant to pointers. Given a type struct foo:
struct foo a = /* initialize */;
struct foo b = a;
All the values in a are copied to b. They are not the same variable.
However, with pointers:
struct foo *p = calloc(1, sizeof *p);
struct foo *q = p;
q now points to the same memory as p; no copying has taken place (and you run the risk of a dangling pointer once one gets freed). This is a pointer alias. In order to do a shallow copy, one would do:
struct foo *p = calloc(1, sizeof *p);
/* assign to p's fields... */
struct foo *q = calloc(1, sizeof *q);
*q = *p;
Now q has the same field values as p but points to a different block of memory.
A deep copy requires additional effort; any pointers in the structure must be traversed and have their contents copied as well. See this post for a good explanation.
When you use newyoob = yoob; the compiler create code to copy the structure for you.
An important note about the copying: It's a shallow. That means if you have e.g. a structure containing pointers, it's only the actual pointers that will be copied and not what they point to, so after the copy you will have two pointers pointing to the same memory.
Your concept of "shallow copy" is wrong. The code
newyoob = yoob;
is in fact creating a shallow copy of yoob to newyoob. Your variables yoob and newyoob are separate memory allocations.
Now if you did this
struct tester* newyoob = &yoob;
Then newyoob and yoob are "the same" - but again, two variables referencing the same memory region is not considered a copy.
Suppose this
typedef struct tester {
int someInt;
char* someString;
} tester;
Shallow copying
Then you assign one instance to another:
tester a = {1, "hohohahah"};
tester b = a;
The members of a will be copied by value, including the pointer. This means:
a.someString == b.someString // True: comparing addresses and addresses are the same.
b is a shallow copy of a because the pointed members point to the same memory.
Deep copying
A deep copy means that the pointed members are also duplicated. It would go along these lines:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct tester {
int someInt;
char* someString;
} tester;
void deepcopy_tester(tester *in, tester *out) {
out->someInt = in->someInt;
out->someString = malloc(strlen(in->someString)+1);
strcpy(out->someString, in->someString);
}
int main() {
tester t1 = {1, "Yo"};
tester t2 = {0, NULL};
deepcopy_tester(&t1, &t2);
printf("%s\na", t2.someString);
}
This code should work, just tested with gcc.

C struct pointer accessing fields

I've read that if we have a pointer to a struct type object we can't access the object field by using the asterisk (*) but we need to use this -> (like an arrow sign).
Now although I read about it I didn't understand why we can't use the asterisk, I'll try to provide a code example and a picture of how I visualize a struct in memory to help you understand what is preventing me from understanding this topic.
Okay, so here's our code :
#include <stdio.h>
typedef struct MyStruct
{
char c1;
char c2;
} MyStruct;
int main()
{
MyStruct s1; // Let's assume s1's address is 0x34A
s1.c1 = 'A';
s1.c2 = 'B';
printf("s1 c1 is %c and c2 is %c.\n", s1.c1, s1.c2);
MyStruct *p = &s1; // Declaring our pointer to point to s1 address, so p points to 0x34A
printf("s1 c1 is %c and c2 is %c.\n", p->c1, p->c2); // I know this is the valid way
printf("s1 c1 is %c.\n", *p.c1); // I don't understand why this would be invalid
return 0;
}
Now let me try to explain why I don't understand why this syntax is invalid.
So p is pointing to the address of s1 which is 0x34A and as I see it this is how I see s1 in memory:
So what I imagine it as in memory is that s1 has it's own address of course which in our example is 0x34A and it points to c1 address in memory which is followed up by c2 address.
So when I do this :
printf("s1 c1 is %c.\n", (*p.c1)++, *p.c2);
I think of it as I'm accessing the value that is inside the pointer address by our pointer so we're accessing the value which is c1.
But as I said it's invalid and I just breaking my head trying to understand why, I've tried to read about it but couldn't really put the finger on the answer that will satisfy me on why really, so I've provided the code example and the picture so you could get into my head and try and see why I'm thinking wrong here.
*p.c1 is parsed as *(p.c1) not (*p).c1.
So when you do *(p.c1), you attempt to use the dot operator on a pointer, which fails.
There is a precedence problem over here. If you want to access like this then go for (*p).c1.
Now by doing (*p), you will reach to your structure and then after members.

How do pointer-to-pointers work in C? (and when might you use them?)

How do pointers-to-pointers work in C?
When might you use them?
Let's assume an 8 bit computer with 8 bit addresses (and thus only 256 bytes of memory). This is part of that memory (the numbers at the top are the addresses):
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| | 58 | | | 63 | | 55 | | | h | e | l | l | o | \0 | |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
What you can see here, is that at address 63 the string "hello" starts. So in this case, if this is the only occurrence of "hello" in memory then,
const char *c = "hello";
... defines c to be a pointer to the (read-only) string "hello", and thus contains the value 63. c must itself be stored somewhere: in the example above at location 58. Of course we can not only point to characters, but also to other pointers. E.g.:
const char **cp = &c;
Now cp points to c, that is, it contains the address of c (which is 58). We can go even further. Consider:
const char ***cpp = &cp;
Now cpp stores the address of cp. So it has value 55 (based on the example above), and you guessed it: it is itself stored at address 60.
As to why one uses pointers to pointers:
The name of an array usually yields the address of its first element. So if the array contains elements of type t, a reference to the array has type t *. Now consider an array of arrays of type t: naturally a reference to this 2D array will have type (t *)* = t **, and is hence a pointer to a pointer.
Even though an array of strings sounds one-dimensional, it is in fact two-dimensional, since strings are character arrays. Hence: char **.
A function f will need to accept an argument of type t ** if it is to alter a variable of type t *.
Many other reasons that are too numerous to list here.
How do pointers to pointers work in C?
First a pointer is a variable, like any other variable, but that holds the address of a variable.
A pointer to a pointer is a variable, like any other variable, but that holds the address of a variable. That variable just happens to be a pointer.
When would you use them?
You can use them when you need to return a pointer to some memory on the heap, but not using the return value.
Example:
int getValueOf5(int *p)
{
*p = 5;
return 1;//success
}
int get1024HeapMemory(int **p)
{
*p = malloc(1024);
if(*p == 0)
return -1;//error
else
return 0;//success
}
And you call it like this:
int x;
getValueOf5(&x);//I want to fill the int varaible, so I pass it's address in
//At this point x holds 5
int *p;
get1024HeapMemory(&p);//I want to fill the int* variable, so I pass it's address in
//At this point p holds a memory address where 1024 bytes of memory is allocated on the heap
There are other uses too, like the main() argument of every C program has a pointer to a pointer for argv, where each element holds an array of chars that are the command line options. You must be careful though when you use pointers of pointers to point to 2 dimensional arrays, it's better to use a pointer to a 2 dimensional array instead.
Why it's dangerous?
void test()
{
double **a;
int i1 = sizeof(a[0]);//i1 == 4 == sizeof(double*)
double matrix[ROWS][COLUMNS];
int i2 = sizeof(matrix[0]);//i2 == 240 == COLUMNS * sizeof(double)
}
Here is an example of a pointer to a 2 dimensional array done properly:
int (*myPointerTo2DimArray)[ROWS][COLUMNS]
You can't use a pointer to a 2 dimensional array though if you want to support a variable number of elements for the ROWS and COLUMNS. But when you know before hand you would use a 2 dimensional array.
I like this "real world" code example of pointer to pointer usage, in Git 2.0, commit 7b1004b:
Linus once said:
I actually wish more people understood the really core low-level kind of coding. Not big, complex stuff like the lockless name lookup, but simply good use of pointers-to-pointers etc.
For example, I've seen too many people who delete a singly-linked list entry by keeping track of the "prev" entry, and then to delete the entry, doing something like:
if (prev)
prev->next = entry->next;
else
list_head = entry->next;
and whenever I see code like that, I just go "This person doesn't understand pointers". And it's sadly quite common.
People who understand pointers just use a "pointer to the entry pointer", and initialize that with the address of the list_head. And then as they traverse the list, they can remove the entry without using any conditionals, by just doing a
*pp = entry->next
Applying that simplification lets us lose 7 lines from this function even while adding 2 lines of comment.
- struct combine_diff_path *p, *pprev, *ptmp;
+ struct combine_diff_path *p, **tail = &curr;
Chris points out in the comments to the 2016 video "Linus Torvalds's Double Pointer Problem".
kumar points out in the comments the blog post "Linus on Understanding Pointers", where Grisha Trubetskoy explains:
Imagine you have a linked list defined as:
typedef struct list_entry {
int val;
struct list_entry *next;
} list_entry;
You need to iterate over it from the beginning to end and remove a specific element whose value equals the value of to_remove.
The more obvious way to do this would be:
list_entry *entry = head; /* assuming head exists and is the first entry of the list */
list_entry *prev = NULL;
while (entry) { /* line 4 */
if (entry->val == to_remove) /* this is the one to remove ; line 5 */
if (prev)
prev->next = entry->next; /* remove the entry ; line 7 */
else
head = entry->next; /* special case - first entry ; line 9 */
/* move on to the next entry */
prev = entry;
entry = entry->next;
}
What we are doing above is:
iterating over the list until entry is NULL, which means we’ve reached the end of the list (line 4).
When we come across an entry we want removed (line 5),
we assign the value of current next pointer to the previous one,
thus eliminating the current element (line 7).
There is a special case above - at the beginning of the iteration there is no previous entry (prev is NULL), and so to remove the first entry in the list you have to modify head itself (line 9).
What Linus was saying is that the above code could be simplified by making the previous element a pointer to a pointer rather than just a pointer.
The code then looks like this:
list_entry **pp = &head; /* pointer to a pointer */
list_entry *entry = head;
while (entry) {
if (entry->val == to_remove)
*pp = entry->next;
else
pp = &entry->next;
entry = entry->next;
}
The above code is very similar to the previous variant, but notice how we no longer need to watch for the special case of the first element of the list, since pp is not NULL at the beginning. Simple and clever.
Also, someone in that thread commented that the reason this is better is because *pp = entry->next is atomic. It is most certainly NOT atomic.
The above expression contains two dereference operators (* and ->) and one assignment, and neither of those three things is atomic.
This is a common misconception, but alas pretty much nothing in C should ever be assumed to be atomic (including the ++ and -- operators)!
When covering pointers on a programming course at university, we were given two hints as to how to begin learning about them. The first was to view Pointer Fun With Binky. The second was to think about the Haddocks' Eyes passage from Lewis Carroll's Through the Looking-Glass
“You are sad,” the Knight said in an anxious tone: “Let me sing you a song to comfort you.”
“Is it very long?” Alice asked, for she had heard a good deal of poetry that day.
“It's long,” said the Knight, “but it's very, very beautiful. Everybody that hears me sing it - either it brings the tears to their eyes, or else -”
“Or else what?” said Alice, for the Knight had made a sudden pause.
“Or else it doesn't, you know. The name of the song is called ‘Haddocks' Eyes.’”
“Oh, that's the name of the song, is it?" Alice said, trying to feel interested.
“No, you don't understand,” the Knight said, looking a little vexed. “That's what the name is called. The name really is ‘The Aged Aged Man.’”
“Then I ought to have said ‘That's what the song is called’?” Alice corrected herself.
“No, you oughtn't: that's quite another thing! The song is called ‘Ways And Means’: but that's only what it's called, you know!”
“Well, what is the song, then?” said Alice, who was by this time completely bewildered.
“I was coming to that,” the Knight said. “The song really is ‘A-sitting On A Gate’: and the tune's my own invention.”
Pointers to Pointers
Since we can have pointers to int, and pointers to char, and pointers to any structures we've defined, and in fact pointers to any type in C, it shouldn't come as too much of a surprise that we can have pointers to other pointers.
Consider the below figure and program to understand this concept better.
As per the figure, ptr1 is a single pointer which is having address of variable num.
ptr1 = &num;
Similarly ptr2 is a pointer to pointer(double pointer) which is having the address of pointer ptr1.
ptr2 = &ptr1;
A pointer which points to another pointer is known as double pointer. In this example ptr2 is a double pointer.
Values from above diagram :
Address of variable num has : 1000
Address of Pointer ptr1 is: 2000
Address of Pointer ptr2 is: 3000
Example:
#include <stdio.h>
int main ()
{
int num = 10;
int *ptr1;
int **ptr2;
// Take the address of var
ptr1 = &num;
// Take the address of ptr1 using address of operator &
ptr2 = &ptr1;
// Print the value
printf("Value of num = %d\n", num );
printf("Value available at *ptr1 = %d\n", *ptr1 );
printf("Value available at **ptr2 = %d\n", **ptr2);
}
Output:
Value of num = 10
Value available at *ptr1 = 10
Value available at **ptr2 = 10
A pointer-to-a-pointer is used when a reference to a pointer is required. For example, when you wish to modify the value (address pointed to) of a pointer variable declared in a calling function's scope inside a called function.
If you pass a single pointer in as an argument, you will be modifying local copies of the pointer, not the original pointer in the calling scope. With a pointer to a pointer, you modify the latter.
A pointer to a pointer is also called a handle. One usage for it is often when an object can be moved in memory or removed. One is often responsible to lock and unlock the usage of the object so it will not be moved when accessing it.
It's often used in memory restricted environment, ie the Palm OS.
computer.howstuffworks.com Link>>
www.flippinbits.com Link>>
it's a pointer to the pointer's address value. (that's terrible I know)
basically, it lets you pass a pointer to the value of the address of another pointer, so you can modify where another pointer is pointing from a sub function, like:
void changeptr(int** pp)
{
*pp=&someval;
}
You have a variable that contains an address of something. That's a pointer.
Then you have another variable that contains the address of the first variable. That's a pointer to pointer.
A pointer to pointer is, well, a pointer to pointer.
A meaningfull example of someType** is a bidimensional array: you have one array, filled with pointers to other arrays, so when you write
dpointer[5][6]
you access at the array that contains pointers to other arrays in his 5th position, get the pointer (let fpointer his name) and then access the 6th element of the array referenced to that array (so, fpointer[6]).
How it works:
It is a variable that can store another pointer.
When would you use them :
Many uses one of them is if your function wants to construct an array and return it to the caller.
//returns the array of roll nos {11, 12} through paramater
// return value is total number of students
int fun( int **i )
{
int *j;
*i = (int*)malloc ( 2*sizeof(int) );
**i = 11; // e.g., newly allocated memory 0x2000 store 11
j = *i;
j++;
*j = 12; ; // e.g., newly allocated memory 0x2004 store 12
return 2;
}
int main()
{
int *i;
int n = fun( &i ); // hey I don't know how many students are in your class please send all of their roll numbers.
for ( int j=0; j<n; j++ )
printf( "roll no = %d \n", i[j] );
return 0;
}
A 5-minute video explaining how pointers work:
There so many of the useful explanations, but I didnt found just a short description, so..
Basically pointer is address of the variable.
Short summary code:
int a, *p_a;//declaration of normal variable and int pointer variable
a = 56; //simply assign value
p_a = &a; //save address of "a" to pointer variable
*p_a = 15; //override the value of the variable
//print 0xfoo and 15
//- first is address, 2nd is value stored at this address (that is called dereference)
printf("pointer p_a is having value %d and targeting at variable value %d", p_a, *p_a);
Also useful info can be found in topic What means reference and dereference
And I am not so sure, when can be pointers useful, but in common it is necessary to use them when you are doing some manual/dynamic memory allocation- malloc, calloc, etc.
So I hope it will also helps for clarify the problematic :)

Resources