I see a code which says the following.
typedef struct dummy
{
int a;
int b[100];
} dummy_t;
typedef struct dummya
{
int a;
int b;
} dummya_t;
void * getptr(){
// return a pointer of a memory
}
void function(){
dummya_t*dstptr = getptr();
dummy_t *srcpt = (dummy_t*)(dstptr->b);
}
I can see dummy_t *srcpt = (dummy_t*)(dstptr->b); meaning as a pointer is pointing to another pointer but then *srcpt should be a double pointer right? Like **srcpt
Your code is not correct, you can not cast nor assign using pointers to objects of different types, for example:
struct a { int a, b; };
struct b { int a, b; };
are different objects even having the same members.
struct a x = {.a = 1, .b = 2};
struct b *y = &x; // wrong
I can see dummy_t srcpt = (dummy_t)(dstptr->b); meaning as a pointer
is pointing to another pointer but then *srcpt should be a double
pointer right? Like **srcpt
Your assumptions are not correct, a basic example using plain pointers to int:
#include <stdio.h>
int main(void)
{
int arr[] = {1, 2};
int *a = &arr[0];
int *b = &arr[1];
printf("a = %d b = %d\n", *a, *b);
int **ptr;
ptr = &a;
*ptr = &arr[1];
ptr = &b;
*ptr = &arr[0];
printf("a = %d b = %d\n", *a, *b);
return 0;
}
The output is:
a = 1 b = 2
a = 2 b = 1
As you can see, you use a double pointer (**ptr) when you want to change the contents of a or b (the address where it points to), a more useful example:
#include <stdio.h>
void swap(int **a, int **b)
{
int *temp = *a;
*a = *b;
*b = temp;
}
int main(void)
{
int arr[] = {1, 2};
int *a = &arr[0];
int *b = &arr[1];
printf("a = %d b = %d\n", *a, *b);
swap(&a, &b);
printf("a = %d b = %d\n", *a, *b);
return 0;
}
Same output:
a = 1 b = 2
a = 2 b = 1
I can see dummy_t *srcpt = (dummy_t*)(dstptr->b); meaning as a pointer is pointing to another pointer but then *srcpt should be a double pointer right? Like **srcpt
No. That line means "treat the value of dstptr->b as though it were a value of type dummy_t *, and assign it to srcpt". It's not a pointer to a pointer.
The expression dstptr->b has type int, which cannot be directly assigned to a pointer type. (dummy_t *) is a cast, which tells the compiler to convert the type.
Like Vlad says in his comment, this code doesn't make any sense. It may compile, but just because it compiles doesn't mean it's right.
Related
I have a question with double pointer to pointer array.
I couldn't understand why is *double_pointer value same with *pointer_array value.
I thought maybe there's something with this line, 'int **pptr = ptr'.
why isn't it 'int **pptr = &ptr' ?
Please help me..
#include <stdio.h>
int main(){
// doulble pointer with array.
int arr[4] = {1,2,3,4};
int *pt = arr;
int **ppt = &pt;
printf("%p, %d\n", *ppt, *pt);
// The result is different. *ppt != *pt
// double pointer with pointer array.
int ptr1[4] = {1,2,3,4};
int ptr2[4] = {5,6,7,8};
int ptr3[4] = {9,10,11,12};
int * ptr[3] = {ptr1, ptr2, ptr3};
int ** pptr = ptr;
// Why is it ptr? I thought it should be &ptr.
printf("%p, %p\n", *pptr, *ptr);
// The result is same. *pptr = *ptr
// Why *pptr is the address of ptr1? It thought should be the address of ptr.
return 0;
}
void s(int* a, int* b) {
a=b;
}
int main(int argc, char* argv[]) {
int* a = malloc(sizeof(int));
int* b = malloc(sizeof(int));
int c = 10;
int d = 5
a = &c;
b = &d;
printf("%d %d\n",*a,*b);
s(a,b);
printf("%d %d\n",*a,*b);
}
I am so very confused. This is very simple code. I thought that this would result in a and b pointing to the same value. When i do a=b inside of the main function everything works as expected. When I use gdb it even shows that they point to the same place in memory and the function is not being optimized away!!! So what is happening?
Is the function creating its own local copy? Why don't these point to the same variable please help.
You want to change the pointers values. Pointers are passed by value, so you need a pointer to the pointer to change its value:
#include <stdio.h>
void s(int** foo, int** bar)
{
*foo = *bar;
}
int main(void)
{
int c = 10;
int d = 5;
int *a = &c;
int *b = &d;
printf("%d %d\n", *a, *b); // 10 5
s(&a, &b);
printf("%d %d\n", *a, *b); // 5 5 a points at d as well
}
With your version you only changed the parameters which are copies of the values passed to the function.
To help you better understand, consider this:
#include <stdio.h>
void value(int foo, int bar)
{
foo = bar; // changing local copies
}
void pointer(int *foo, int *bar)
{
*foo = *bar; // changing the value foo points to to the value bar points to
}
int main(void)
{
int a = 5;
int b = 7;
value(a, b);
printf("%d, %d\n", a, b); // 5, 7
pointer(&a, &b);
printf("%d, %d\n", a, b); // 7, 7
}
We did that with the type int. Now lets just replace int with int*:
#include <stdio.h>
void value(int *foo, int *bar)
{
foo = bar; // changing local copies
}
void pointer(int **foo, int **bar)
{
*foo = *bar; // changing the value foo points to to the value bar points to
}
int main(void)
{
int x = 5;
int y = 7;
int *a = &x;
int *b = &y;
value(a, b);
printf("%d, %d\n", *a, *b); // 5, 7
pointer(&a, &b);
printf("%d, %d\n", *a, *b); // 7, 7 now both point at y
}
So you see, it's the same concept both times. In the first example the values pointed to are ints and their values are numbers, in the second example the values pointed to are int*s and their values are pointer values (<~ standard terminology, "addresses"). But the mechanism is the same
You program is almost correct but you need to pass the address of the variables in the function as you using call by refrence and in the function take pointer to pointers.
I started learning C this week and here is the first difficulty I'm encountering with pointers.
I would like to test this function:
void ft_ultimate_div_mod(int *a, int *b)
{
int *tmp;
if (b != 0)
{
*tmp = *a % *b;
*a = *a / *b;
*b = *tmp;
}
}
But I can't figure out the main using pointers. My program is not printing anything with:
int main(void)
{
int *a;
int *b;
*a = 5;
*b = 3;
ft_ultimate_div_mod(a, b);
printf("%d", *a);
printf("%d", *b);
return (0);
}
What can I do? Thx!
For starters it does not make sense to declare the variable tmp as having the type int *. The variable is used to store an object of the type int so it has to have the type int. Otherwise you are using an uninitialized pointer with an indeterminate value that results in undefined behavior of the program.
Also as the variable is used in the compound statement of the if statement then it should be declared in the blco scope of the if statement.
These declarations in main
int *a;
int *b;
als do not make sense.
What you mean is the following.
#include <stdio.h>
void ft_ultimate_div_mod(int *a, int *b)
{
if ( *b != 0 )
{
int tmp = *a % *b;
*a = *a / *b;
*b = tmp;
}
}
int main(void)
{
int a = 10;
int b = 3;
ft_ultimate_div_mod( &a, &b );
printf( "%d %d\n", a, b );
return 0;
}
The function ft_ultimate_div_mod excepts its arguments by reference because it tries to change their original values.
You declare int *tmp in your function. That is a pointer to an int sized bit of memory that can store an int. You never point it anywhere however so *tmp = *a % *b; will do a mod of a an b and then crash when it stores it using an uninitialized pointer.
Change the *tmp to just tmp so then your code becomes:
void ft_ultimate_div_mod(int *a, int *b)
{
int tmp;
if (a && b)
{
tmp = *a % *b;
*a = *a / *b;
*b = tmp;
}
}
I write a C program. I have 2 pointers *a and *b. Now, I impose *a = 20 and *b = 10. After that I impose a = b in subroutine but the value seem doesn't change. I expect that *a = 10 and *b = 10. Please help me find a solution for this. Thank you.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void Copy(int *a1, int *a2)
{
a1 = a2;
}
void Test()
{
// a = 20
int *a = (int *)malloc(sizeof(int));
*a = 20;
// b = 10
int *b = (int *)malloc(sizeof(int));
*b = 10;
Copy(a, b); // a = b
printf("\na = %d", *a);
printf("\nb = %d", *b);
}
int main(int argc, char *argv[]){
Test();
fflush(stdin);
printf("\nPress any key to continue");
getchar();
return 0;
}
Function arguments are passed by value. So inside the function Copy you are modifying a copy of the pointer not the original pointer you had in the Test function. So when you leave the Copy function the pointers outside are not modified.
Change the Copy to receive a int **, so you can change inside the value of pointers
void Copy(int **a1, int **a2)
{
*a1 = *a2;
}
And call the function in this way:
void Test() {
...
Copy (&a, &b);
...
}
If you want to assign the values, such that after the call of Copy *a == *b holds, then you'd have to change your function as follows:
void Copy(int *a1, int *a2)
{
*a1 = *a2;
}
If you want to assign the pointers, such that after the call of Copy a==b holds, then you have to pass a pointer to the pointers:
void Copy(int **a1, int **a2)
{
*a1 = *a2;
}
void Test() {
...
Copy (&a, &b);
...
}
Note that a==b also implies that *a == *b.
I have code snippet that I can't understand how it works, because of one line that does a double dereference. The code looks like this:
void afunction(int**x){
*x = malloc(2 * sizeof(int));
**x = 12;
*(*x + 1) = 13;
}
int main(){
int *v = 10;
afunction(&v);
printf("%d %d\n", v[0], v[1]);
return 1;
}
I understand that the first element of the pointer to pointer gets the value 12, but the line after that I just can't seem to understand. Does the second element in the first pointer get value 13?
The code is rather easy to understand if you use a temporary variable, eg:
void afunction(int**x)
{
int* t = *x;
t = malloc(2 * sizeof(int));
*t = 12;
*(t+1) = 13;
}
so:
x is a pointer to a pointer to int
*x yields a int* (pointer to int)
**x = is like *(*x) = so you first obtain the pointer to int then by dereferencing you are able to set the value at the address
The last part *(*x+1) = can be broken down:
int* pointerToIntArray = *x;
int* secondElementInArray = pointerToIntArray + 1;
*secondElementInArray = 13;
The purpose of using a pointer to pointer here is that you can pass the address to an int* to the function and let the function allocate the memory and fill it with data. The same purpose could be done by returning an int*, eg:
int* afunction() {
int* x = malloc(sizeof(int)*2);
*x = 12;
*(x+1) = 13;
return x;
}
int main() {
int* v = afunction();
return 0;
}