pointer: *p and &p in C programming language [duplicate] - c

This question already has answers here:
C++ - *p vs &p vs p
(5 answers)
Closed 8 years ago.
Could you please tell me the difference between *p and &p in C programming language? cause I really have problem with this and I don't know whether *p or &p is ok!!!!

Just take
int a =10;
int *p = &a;
a is a variable which holds value 10. The address in which this value is stored is given by &a.
Now we have a pointer p , basically pointer points to some memory location and in this case it is pointing to memory location &a .
*p gives you 10
This is called dereferencing a pointer.
p = &a /* Gives address of variable a */
Now let's consider
&p
Pointer is a also a data-type and the location in which p is stored is given by &p

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store any variable address. The general form of a pointer variable declaration is:
type *var-name;
eg:
int *ip; /* pointer to an integer */
double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointer to a character */
Look at this program :
#include <stdio.h>
int main ()
{
int var = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */
ip = &var; /* store address of var in pointer variable*/
printf("Address of var variable: %x\n", &var );
/* address stored in pointer variable */
printf("Address stored in ip variable: %x\n", ip );
/* access the value using the pointer */
printf("Value of *ip variable: %d\n", *ip );
return 0;
}
https://www.tutorialspoint.com/cprogramming/c_pointers.htm

Related

How to use memory address to dereference? [duplicate]

This question already has answers here:
Pointers in C: when to use the ampersand and the asterisk?
(10 answers)
Closed 14 days ago.
I create a pointer and get its memory address. How can i dereference the pointer?
#include <stdlib.h>
int main()
{
int b[] = {11,22,33,44,55};
int *a = b;
printf("memory:%p\n",a);
return(0);
}
suppose the memory address of b is 0x7fff24e69e60. How can i get the b[0] by the memory address?
How to use memory address to dereference in C language?
In C programming, to dereference a memory address, you use the * operator.
int x = 5;
int *ptr = &x;
printf("Value of x: %d\n", x);
printf("Value stored at address %p is: %d\n", ptr, *ptr);
Here, ptr is a pointer to an integer and is assigned the address of x using the & operator. The *ptr expression dereferences the pointer and accesses the value stored at the memory address ptr.

dereferencing pointer to a pointer with one Asterisk [duplicate]

This question already has answers here:
Pointer to pointer clarification
(16 answers)
Closed 1 year ago.
#include <stdio.h>
int main(){
int integer = 300;
int* p = &integer;
int** pp = &p;
printf("*pp = %i", *pp);
}
My question is what is actually *pp? What does it mean to print the value in the address of *p ?
It is a reference to the integer converted to an integer value.
To print the reference you should use %p format specifier instead of the %i format specifier.
[pp] -> [p] -> [integer]
If you modify the program you can see the references and how they are chained:
int main()
{
int integer = 300;
int* p = &integer;
int** pp = &p;
printf("pp = %p *pp = %p\n",(void *)pp, (void *)*pp);
printf("&integer = %p &p = %p\n", (void *)&integer, (void *)&p);
}
pp = 0x7fffb6adc7d8 *pp = 0x7fffb6adc7e4
&integer = 0x7fffb6adc7e4 &p = 0x7fffb6adc7d8
#include <stdio.h>
int main(){
int integer = 300;
int* p = &integer;
int** pp = &p;
printf("**pp=%d\n", **pp);
printf("*p=%d\n", *p);
printf("*pp = %p\n", *pp);
printf("p=%p\n", p);
printf("&integer=%p\n", &integer);
printf("&(*p)=%p\n", &(*p));
printf("pp=%p\n", pp);
printf("&p=%p\n", &p);
}
Since &(*p) is simply p, yes *pp is the address of *p.
Since %i is integer but what you want to print is pointer. So I changed it to %p.
I'm not a pro in C programming, but I believe I have a decent knowledge of pointers.
So, to understand what is *pp, you need to understand what is * operator used for?
It is no doubt used for multiplication, but it is also used for pointer de-referencing.
So when I say *pp, it means the value of pp i.e. what is stored in pp. Each defined variable in C will have a memory space of its own. So *pp means what is stored in the memory space that was given to the variable pp.
In your case,
int integer = 300;
A variable of type integer named integer is declared and given the value 300
int* p = &integer;
A pointer that needs to point to an integer variable is declared and given the address of integer.
int** pp = &p;
A pointer that needs to point to an integer variable is declared and given the address of p
printf("*pp = %i", *pp);
*pp means printing the value which is stored in pp i.e. address of p
**pp means printing the value which is stored in the value which is stored in p i.e. 300
As I explained earlier, each variable has a memory space of its own, and each memory space will have an address assigned to it, so that it can be identified (the way we have an email address which is used to contact us via email or a postal address which is used to contact us via post).
Hope this answers your question. Do ask follow-up questions if any.
Cheers..!!

C: given known address and type, how to print values stored in it?

I am doing a homework of C programming.
#include <stdio.h>
typedef struct JustArray {
char *x;
} JustArray;
int main()
{
JustArray Items[12];
char *d = "Test";
Items[0].x = (char *) &d;
printf("Pointer: %p\n", &d);
printf("Address: %u\n",&d);
printf("Value: %s\n", d);
/*------------------------------------*/
//Knowing address of d from above, print value stored in it using Items[0].x. Cannot use &d, *d, or d.
char *ptr;
ptr = Items[0].x;
printf("%p\n", Items[0].x);
printf("%p\n", &ptr);
printf("%s\n", ptr);
return 0;
}
The output of ptr needs to be "Test" as well but it is showing me weird characters. The assignment is to have find a way to use the address info in Items[0].x and print its value "Test" in console. I could not find a way to do it...
Remember that the value of a pointer is an address. The content of a pointer (i.e. its dereference, the value pointed, addressed by it) is a value (with the type being pointed) which is in another place. So a int* has as value an address of another variable (of type int) which is stored in another address. Note that each primitive variable in C is the name of a data stored in some address.
int p = 1;
p above is a var of type int. It has 1 as value, which is directly given by p (so p == 1). p has an address (say 0x11111) that can be accessed with &p (so &p == 0x11111). Thus, p is the name of a variable whose value is an int (1) and whose address is 0x11111 (given by &p). This means that the value 1 (which is an int) is stored in the address 0x11111.
int* q = 0x22222;
q above is a var of type int* (pointer to int, meaning "an address to a var of type int"). q has an address as value (in this case is 0x22222), which is directly given by q (so q == 0x22222). but q also has an address (say 0x33333), which can be accessed with &q (so &q == 0x33333). Thus, q is the name of a variable whose value is an address to int (0x22222, given by q) and whose address is 0x33333 (given by &q). Basically, the value 0x22222 (which is an address) is stored in the address 0x33333.
You can use the prefix operator * over q to dereference it, i.e. access its content, which is the value stored in the address 0x22222, which is the value of q. So basically we have: q==0x22222, &q==0x33333, and *q==[whatever is the value stored in the adress 0x22222]. Now consider this:
int* r = &p;
Remember that p has value 1 (an int given by p itself) and address 0x11111 (given by &p). Now I declared a pointer to int called r and initialized it with the address of p (0x11111). Thus, r is a pointer whose value is an adress (0x11111), but it also has an address (say 0x44444).
The operator * prefixing a pointer lets us access the value of the address that is its value. So *r will give us 1 because 1 is the value stored in the address 0x11111, which is the value of r and the address of p at the same time. So r==&p (0x11111==0x11111) and *r==p (1==1).
Now lets go back to your code. When you declare a char* you already have a pointer, so the printing of it will be an address if you set %p as the desired format or a string if you set %s as the desired format. (The %s makes the function printf iterate throughout the contents whose start address is the value of the given pointer (and it stops when it reaches a '\0'). I fixed your code (and modified it a little bit for the sake of readability). The problem was that you were sending the addresses of the pointers instead of the pointers themselves to the function printf.
#include <stdio.h>
typedef struct justArray
{
char* x;
}
JustArray;
int main()
{
//d is a pointer whose content is an address, not a char 'T'
char* d = "Test";
printf("Pointer: %p\n", d); //print the address (no need for &)
printf("Address: %lu\n", (long unsigned) d); //print the address again as long unsigned)
printf("Value: %s\n", d); //print a string because of the %s
JustArray Items[12];
Items[0].x = d;
printf("%p\n", Items[0].x); //Items[0].x == d, so it prints the same address
char* ptr = Items[0].x;
printf("%p\n", ptr); //ptr == Items[0].x, so it prints again the same address
printf("%s\n", ptr); //prints the string now because of the %s
return 0;
}
The output of this program will be:
Pointer: 0x400734
Address: 4196148
Value: Test
0x400734
0x400734
Test
The key here is to notice what this line does:
Items[0].x = (char *) &d;
Observe first that d is a char * (i.e. a null-terminated string in this case), thus &d (taking a reference to it) must yield a value of type char * * (i.e. a pointer to a pointer to a char, or a pointer to a string). The cast to char * has to be done for the C type checker to accept the code. [Note that this is considered very bad practice, since you are forcing a value of one type into a value of another type that makes no sense. It's just an exercise though, and it's precisely this "bad practice" that makes it a bit of a challenge, so let's ignore that for now.]
Now, to get back d again, given just Items[0].x, we need to sort of "invert" the operations of the above line of code. We must first convert Items[0].x to its meaningful type, char * *, then dereference the result once to get a value of type char * (i.e. a null-terminated string).
char *d_again = *((char * *) Items[0].x);
printf("%s\n", d_again);
And viola!

Crashing code(pointers C99)

I am trying to understand why that code crash:(CodeBlocks C99)
int a;
int **b=0;
*b=&a;
I know that *b is of type int* and &a is also int* so what's the problem here?
Let's take this apart:
int a; /* make a an integer, assuming this is in a function on the stack */
int **b=0; /* make b a pointer to a pointer to an integer, but make it point to 0 */
*b=&a; /* store the address of a at the address pointed to by b, which is 0 */
IE you are explicitly writing the address of a to a zero location. The problem is not type compatibility, it's that you are trying to store something at location zero, which will cause a seg fault.
To fix it do something like:
int a; /* make a an integer, assuming this is in a function on the stack */
int *c = 0; /* make an integer pointer, initialise it to NULL */
int **b=&c; /* make b a pointer to a pointer to an integer, make it point to c */
*b=&a; /* store the address of a at the address pointed to by b, which is c */
b points to a pointer, you point the pointer to 0 / NULL, meaning when you do *b = you are assigning the value to address 0 which will die on most OSs ( on embedded systems this can be valid depending on the processor)
You are dereferencing the NULL-Pointer. b points to NULL and in the next line you are dereferencing it and assigning it a new value.
You are not allowed to write to memory you don't own, and you are especially not allowed to write to NULL.
You cannot set pointers to values like that (*b=&a) because they are at the time not pointing to anything; in order to set their value they must be pointing to something.
int a;
int *tmp;
int **b = &tmp;
*b = &a; //this should work because you are setting a real variable (in this case tmp) to &a
Your int **b=0 is a pointer to int *, that is initialized to NULL, and isn't pointing at an allocated storage area. When you write to *b, you are attempting to dereference a null pointer, which is illegal.
If you were to allocate storage, for example using b=malloc(sizeof(*b)), you would then be able to write into the area pointer at by b using *b=&a, because then *b would be a valid address to write into.
#include <stdlib.h>
int
main()
{
int a;
int **b = (int**)malloc(1 * sizeof(int));
*b = &a;
free(b);
return 0;
}
Your *b = &a has the same effect of b[0] = &a.

What is the difference in C between &i and i if i is an array of integers? [duplicate]

This question already has answers here:
How come an array's address is equal to its value in C?
(6 answers)
Closed 9 years ago.
I tried a code to see what is the difference between &i and i if i is an array. My assumption was that i represents the starting address of the array, and I had no idea what will happen if I print &i as well.
The result was surprising (to me), as both i and &i was the starting address of the array.
#include<stdio.h>
int main()
{
char str[25] = "IndiaBIX";
int i[] = {1,2,3,4};
printf("%i\n", &i);
printf("%i\n", i);
return 0;
}
The result will be:
2686692
2686692
This is the same address.
But if I use a pointer to int:
#include<stdio.h>
#include <stdlib.h>
int main()
{
int *j = (int *) malloc(sizeof(int));
*j = 12;
printf("%i %i %i\n", *j,j,&j);
return 0;
}
The result will be:
12
5582744
2686748
Here I assume the first is the value of the memory area pointed to by j,the second is the address of the memory area pointed to by j, the third is the memory address of the pointer itself. If I print i and &i, why does &i not mean the memory address of the pointer i?
int i[] = {1,2,3,4};
The difference is their type, i has a type of integer array, &i has a type of a pointer of an integer array.
Yeah, both i and &i leads to print the same answer but they are not exactly same,
-> i represents the address of the first element in an array named i.
-> &i represents the address of the whole array(though values of both are same, their types are different)
For more info, please refer this [link]http://publications.gbdirect.co.uk/c_book/chapter5/arrays_and_address_of.html
int ar[10];
ip = ar; /* address of first element */
ip = &ar[0]; /* address of first element */
ar10i = &ar; /* address of whole array */

Resources