Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C Exercises: Show the basic declaration of pointer

C Pointer : Exercise-1 with Solution

Write a program in C to show the basic declaration of pointer.

Pictorial Presentation:

C Exercises: Pictorial: Show the basic declaration of pointer.

Sample Solution:

C Code:

#include <stdio.h>
void main(void)
{
int m=10,n,o;
int *z=&m ;

	printf("\n\n Pointer : Show the basic declaration of pointer :\n");
	printf("-------------------------------------------------------\n"); 
	printf(" Here is m=10, n and o are two integer variable and *z is an integer");	
	printf("\n\n z stores the address of m  = %p\n",  z); // z is a pointer so %p would print the address
	printf("\n *z stores the value of m = %i\n",   *z); 
	printf("\n &m is the address of m = %p\n",   &m); // &m gives the address of the integer variable m 
                             // so %p is the specifier for that address
	printf("\n &n stores the address of n = %p\n",   &n);
	printf("\n &o  stores the address of o = %p\n",   &o);
	printf("\n &z stores the address of z = %p\n\n", &z); // &z gives the address, where the pointer z is 
                             // stored -> still an address -> %p is the right
                             // specifier        
}

Sample Output:

 Pointer : Show the basic declaration of pointer :                                                            
-------------------------------------------------------                                                       
 Here is m=10, n and o are two integer variable and *z is an integer                                          
                                                                                                              
 z stores the address of m  = 0x7ffd763082b4                                                                  
                                                                                                              
 *z stores the value of m = 10                                                                                
                                                                                                              
 &m is the address of m = 0x7ffd763082b4                                                                      
                                                                                                              
 &n stores the address of n = 0x7ffd763082b8                                                                  
                                                                                                              
 &o  stores the address of o = 0x7ffd763082bc                                                                 
                                                                                                              
 &z stores the address of z = 0x7ffd763082c0   

Flowchart:

Flowchart: Show the basic declaration of pointer

C Programming Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: C Pointer Exercises Home
Next: Write a program in C to demonstrate how to handle the pointers in the program.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP