Pointer operators
* and & q = *m * unary operator returns the value located at the address. You can think * as at address. m = &count & return the memory address of its operand. You can think of & as returning the address of.
The pointer ptr is located at memory address 100 x is located at memory address 200
int x, *ptr;
ptr=&x
& returns the memory address of its operand.
x=5 or *ptr=5
x=ptr;
This first one changes the value of x to y, while the second one changes the value of y to the new value of x, which is the same as y, and hence does not change its value.
printf("\n\n"); for (i = 0; i < 6; i++) { printf("my_array[%d] = %d ",i,my_array[i]); printf("ptr + %d = %d\n",i, *(ptr + i)); } return 0; }
/*<-- A */ /*<-- B */
Different pointers
1.What is pointer? Pointer holds memory address Example: Int *ptr; 2.What is constant pointer? A const pointer means you cannot change the pointer variable itself but you can change the value it points to Example:Char *const argv[ ]; 3.What is pointer to constant? A pointer to constant means you can change the pointer but not what it points to Example: Const char *arg;
4.What is void pointer? A pointer which accepts any data type. A void pointer is used for working with raw memory or for passing a pointer to an unspecified type. Example: Void *ptr; 5.What is double pointer? good use for pointers to pointers is in dynamically allocated, simulated multidimensional arrays Example: Int **ptr; 6. What is function pointer? Function pointer definition: A pointer which keeps address of a function is known as function pointer. Example: void (*foo)(int);
2D array representation
2D array representation
calloc
Allocates space for an array of elements initializes them to zero and returns a pointer to the memory
free
realloc
ptr=(cast-type*)malloc(byte-size); ptr is a pointer of type cast-type the malloc returns a pointer (of cast type) to an area of memory with size byte-size. x=(int *)malloc(100*sizeof(int)); On successful execution of this statement a memory equivalent to 100 times the area of int bytes is reserved and the address of the first byte of memory allocated is assigned to the pointer x of type int
Auto variables
Extern variables
Static variables
Register variables
char copy(char []);//protype declaration OR char copy(char *);//protype declaration Int main() { int i; char a ,*ip; ip = (char *)malloc(5*sizeof(char)); for(i=0;i< 5;i++) { scanf(" %c",&ip[i]); } a = copy(ip);//call to copy function from main printf("\nvalue in main is %c",a); return 0; } copy(char *ip) { int i; for(i=0;i<5;i++) { printf("in function %c\n",*(ip+i)); } return (*ip); }
In the following code explain both copy function signatures i.e copy(char *ip) and copy(char ip[])
OR
Bit operations in C
Bit operations in C
Bit operations in C