- the value stored (rvalue) and the address (lvalue) at which the value is stored and the name(variable name) which is mapped(in symbol table) to this address
- variable for storing lvalue i.e. address is “pointer”
- address of operator is ‘&’ , to get the address of any variable
- dereferencing operator is ‘*’ , to get the value which the pointer variable is pointing
- pointer arithmetic involves modifying address
- so for a char* cptr saying cptr + 1, will increment the address by one (sizeof char)
- for a int* iptr saying iptr + 1, will increment the address by four (sizeof int)
- array (contiguous memory allocation) indexing works because of pointer arithmetic
- int array[] { 1,2}, “array” (pointing to first address of the contiguous memory allocated) is constant
- in C (as mentioned in K&R II) “object is a named region of storage, and a lvalue is an expression referring to the object”
- arrays can be called as un-modifiable lvalue
- iptr = &array[0] , and iptr = array , both means the same according to the C standard, which give rise to the confusion that array is a pointer
- remember we cant do array = iptr (because “array” is a constant, un-modifiable lvalue)
- variables are in data segment and constants are in code segment
- name of the variable can be interpreted in two ways
- when used on the left side of the assignment operator, for compiler its a memory location
- when used on the right side, for compiler it means the content stored at the memory location
- as “array” is actually array[0] (address of the first element), the compiler puts this address in the code segment, and uses it whenever it sees iptr = array in the code.
- “nul” is ‘\0’ character and NULL is macro (in stdio.h)
- void *vptr is a generic pointer, and any type of pointer can be compared with the void pointer
- in C string is an array of characters with nul (‘\0’) to mark the end
- while(*dest++ = *src++), does three things, in sequence
- copy the data src is pointing to dest
- increment dest and src
- check if the dest is true (‘\0’ is false), and continues the loop
- array[i] is same as *(iptr + i), this doesn't mean array and pointers are same, both will identify the same value
- “while(dest[i] = src[i++]);” can be written as “while(i[dest] = i++[src]);”
1: #include <stdio.h>
2:
3: int main(int argc, char **argv)
4: {
5: int j = 1;
6: int k = 2;
7: int *ptr = &k;
8: int array[] = {1,2,3,4,5,6,7,8,9,'\0'};
9: int index = 0;
10: printf("value at j is %d, address of j is %p \n",j,&j);
11: printf("value at k is %d, address of k is %p \n",k,&k);
12: printf("value(address stored) at ptr is %p, address of ptr is %p \n",ptr,&ptr);
13: printf("ptr is pointing to value %d\n",*ptr);
14: printf("sise of ptr is %d \n",sizeof ptr);
15:
16: while(array[index] != '\0')
17: printf("%d ",array[index++]);
18:
19: printf("\n");
20:
21: ptr = &array[0];
22:
23: while(*ptr)
24: printf("%d ",*ptr++);
25:
26: printf("\n");
27:
28: ptr = array;//same as ptr = &array[0]
29:
30: for(index = 0 ; index < 10; index++)
31: {
32: //printf("ptr + %d = %d \n",index,*(ptr + index));
33: printf("ptr + %d = %d \n",index,*ptr++);
34: //printf("ptr + %d = %d \n",index,*(++ptr));
35: }
36:
37: char *cptr = NULL;
38: void *vptr;
39: long *lptr;
40: double *dptr;
41:
42: printf("size of vptr is %d \n",sizeof vptr);
43: printf("size of lptr is %d \n",sizeof lptr);
44: printf("size of dptr is %d \n",sizeof dptr);
45: printf("size of cptr is %d \n",sizeof cptr);
46:
47: char *pmname, *puname;//variables
48: char mname[] = "my name is just a name";//constant
49: char uname[80] = "12345678901234567890123456789012345678901234567890";//constant
50:
51: pmname = mname;
52: puts(mname);
53: puts(pmname);
54: puname = uname;
55: puts(uname);
56: puts(puname);
57: while(*puname++ = *pmname++);//fist copy then increment then check for true
58: puts(uname);
59: pmname = uname;
60:
61: while(*pmname)
62: {
63: printf("%c \t",*pmname);//character value at pa
64: printf("%d \t",*pmname);//ascii value at pa
65: printf("%p \t",&pmname);//cant modify,
66: printf("%p \n",pmname);//the address which is stored at pmname
67: pmname++;
68: };
69: printf("%c \t",*pmname);//character value at pa
70: printf("%d \t",*pmname);//ascii value at pa
71: printf("%p \t",&pmname);//cant modify,
72: printf("%p \n",pmname);//the address which is stored at pmname
73:
74: printf("hello world\n");
75: getchar();
76: return 0;
77: }
78:
No comments:
Post a Comment