Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //public domain
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /*
- (1) int var;
- (2) int var = 123;
- (3) &var
- (4) int* pnt;
- (4) *pnt
- (5) pnt = &var;
- (6) *pnt = var;
- (7) char str[] = "Zeppelin";
- (8) char arr[10]={0}; arr[0] = 'Z'; arr[1] = 'e';
- (9) char str[] = "Zeppelin"; address of str &str &str[0]
- (10) str[0]; str[1];
- (11) char str[] = "Zeppelin"; *str;
- (12) char str[] = "Zeppelin"; char* pnt;
- (13) char str[] = "Zeppelin"; *str; *(str+1); *(str+2);
- (14) char arr[3][3]={{'Z','e','p'},{'p','e','l'},{'i','n','\0'}};
- (15) char** arr;
- (16) char** arr; with realloc
- (17) char (*buff)[80];
- (18) char** int** double** sizeof
- (19) char* int* float* double* sizeof
- (20) function pointer
- (21) struct, st.a
- (22) typedef struct, st.a
- (23) struct stru* pSt; pSt=&st; pSt->a
- (24) struct stru* st; sizeof structure, malloc
- (25) various struct pointers and sizeof structure
- (26) n[arr]
- (27) pnt++; pnt--;
- (28) typedef name char[MAX];
- (29) malloc (sizeof(arr))
- (30) array size, address
- (31) pnt++; while
- (32) swap(char** a, char** b) swap(&strArr[0], &strArr[1]);
- (33) int arr[]={1,2,3}; int *ptr; ptr = &arr[0]; *(ptr) *(ptr+1)
- (34) char *pnt; malloc(); pnt[i];
- (35) int *pnt; malloc(); pnt[i];
- */
- void testfncA()
- {
- printf("Zeppelin\n");
- }
- void testfncB(char c)
- {
- printf("%c\n", c);
- }
- void testfncC(int x, int y)
- {
- printf("%d %d\n", x, y);
- }
- int testfncD(int x, int y)
- {
- return x + y;
- }
- void testfncE(int* a)
- {
- *a+=1;
- }
- void swap(char** a, char** b)
- {
- char* tmp;
- tmp = *a;
- *a = *b;
- *b = tmp;
- }
- int main (int argc, char **argv){
- //-----(1)-----
- {
- int var;
- var = 123;
- printf("(1) value of var = %d\n", var);
- }
- //-----(2)-----
- {
- int var = 123;
- printf("(2) value of var = %d\n", var);
- }
- //-----(3)-----
- {
- int var = 123;
- printf("(3) value of var = %d\n", var);
- printf("(3) address of var = %p\n", (void*) &var);
- }
- //-----(4)-----
- {
- int* pnt;
- pnt = malloc(sizeof *pnt);
- if(pnt)
- {
- *pnt = 123;
- printf("(4) value of pnt = %d\n", *pnt);
- printf("(4) address of pnt = %p\n", (void*) pnt);
- free(pnt);
- }
- }
- //-----(5)-----
- {
- int var = 123;
- int* pnt;
- pnt = &var;
- printf("(5) value of pnt = %d\n", *pnt);
- printf("(5) address of pnt = %p\n", (void*) pnt);
- }
- //-----(6)-----
- {
- int var = 123;
- int* pnt;
- pnt = malloc(sizeof(*pnt));
- if(pnt)
- {
- *pnt = var;
- printf("(6) value of pnt = %d\n", *pnt);
- printf("(6) address of pnt = %p\n", (void*) pnt);
- free(pnt);
- }
- }
- //-----(7)-----
- {
- char str[] = "Zeppelin";
- printf("(7) %s\n", str);
- }
- //-----(8)-----
- {
- char arr[9]={0};
- arr[0] = 'Z';
- arr[1] = 'e';
- arr[2] = 'p';
- arr[3] = 'p';
- arr[4] = 'e';
- arr[5] = 'l';
- arr[6] = 'i';
- arr[7] = 'n';
- //null terminated, because arr[8] is 0
- printf("(8) %s\n", arr);
- }
- //-----(9)-----
- {
- char str[] = "Zeppelin";
- printf("(9) address: %p\n", (void*) str); //address of array
- printf("(9) address: %p\n", (void*) &str);
- printf("(9) address: %p\n", (void*) &str[0]); //address of first element of an array
- printf("(9) address: %p\n", (void*) &str[1]); //address of second element of an array
- }
- //-----(10)-----
- {
- char str[] = "Zeppelin";
- printf("(10) %c\n", str[0]);
- printf("(10) %c\n", str[1]);
- }
- //-----(11)-----
- {
- char str[] = "Zeppelin";
- printf("(11) %c\n", *str);
- printf("(11) %c\n", str[0]);
- printf("(11) %c\n", str[1]);
- }
- //-----(12)-----
- {
- char str[] = "Zeppelin";
- char* pnt;
- pnt = str;
- printf("(12) %s\n", pnt);
- }
- //-----(13)-----
- {
- char str[] = "Zeppelin";
- printf("(13) %c %c %c %c %c %c %c %c\n", *str, *(str+1), *(str+2), *(str+3), *(str+4), *(str+5), *(str+6), *(str+7));
- }
- //-----(14)-----
- {
- char arr[3][3]={{'Z','e','p'},{'p','e','l'},{'i','n','\0'}};
- printf("(14) %c%c%c%c%c%c%c%c\n", arr[0][0], arr[0][1], arr[0][2], arr[1][0], arr[1][1], arr[1][2], arr[2][0], arr[2][1]);
- }
- //-----(15)-----
- {
- char** arr;
- char str1[]="Zepp";
- char str2[]="elin";
- arr = (char**) malloc(sizeof *arr *2);
- if(arr)
- {
- arr[0] = malloc(strlen(str1)+1);
- if(arr[0])
- {
- //memset(arr[0] + strlen(str1), 0, 1);
- arr[1] = malloc(strlen(str2)+1);
- if(arr[1])
- {
- //memset(arr[1] + strlen(str2), 0, 1);
- //strcpy() function copies the string, including the terminating null byte.
- strcpy(arr[0],str1);
- strcpy(arr[1],str2);
- printf("(15) %c%c%c%c%c%c%c%c\n", arr[0][0], arr[0][1], arr[0][2], arr[0][3], arr[1][0], arr[1][1], arr[1][2], arr[1][3]);
- printf("(15) %s%s\n",arr[0],arr[1]);
- free(arr[1]);
- }
- free(arr[0]);
- }
- free(arr);
- }
- }
- //-----(16)-----
- {
- char** arr = NULL;
- char** arr_tmp;
- char str1[]="first";
- char str2[]="second";
- char str3[]="third";
- int cnt = 0;
- /*
- void *realloc(void *ptr, size_t size);
- if ptr is a null pointer, realloc() behaves like malloc()
- */
- arr = (char**) realloc(NULL,sizeof *arr *(cnt+1));
- if(!arr)
- {
- printf("ERROR! arr (%d) memory not allocated.\n",cnt);
- goto lbl_exit;
- }
- arr[cnt] = malloc(strlen(str1)+1);
- if(!arr[cnt])
- {
- printf("ERROR! arr[%d] memory not allocated.\n",cnt);
- goto lbl_exit;
- }
- //strcpy() function copies the string, including the terminating null byte.
- strcpy(arr[cnt],str1);
- cnt++;
- arr_tmp = (char**) realloc(arr,sizeof *arr *(cnt+1));
- if(!arr_tmp)
- {
- printf("ERROR! arr (%d) memory not allocated.\n",cnt);
- goto lbl_exit;
- }
- arr = arr_tmp;
- arr[cnt] = malloc(strlen(str2)+1);
- if(!arr[cnt])
- {
- printf("ERROR! arr[%d] memory not allocated.\n",cnt);
- goto lbl_exit;
- }
- //strcpy() function copies the string, including the terminating null byte.
- strcpy(arr[cnt],str2);
- cnt++;
- arr_tmp = (char**) realloc(arr,sizeof *arr *(cnt+1));
- if(!arr_tmp)
- {
- printf("ERROR! arr (%d) memory not allocated.\n",cnt);
- goto lbl_exit;
- }
- arr = arr_tmp;
- arr[cnt] = malloc(strlen(str3)+1);
- if(!arr[cnt])
- {
- printf("ERROR! arr[%d] memory not allocated.\n",cnt);
- goto lbl_exit;
- }
- //strcpy() function copies the string, including the terminating null byte.
- strcpy(arr[cnt],str3);
- cnt++;
- printf("(16) %s %s %s\n", arr[0], arr[1], arr[2]);
- lbl_exit: ;
- if(arr){
- for(int i = 0;i<cnt;i++)
- {
- if(arr[i]) free(arr[i]);
- }
- free(arr);
- }
- }
- //-----(17)-----
- {
- char (*buff)[80];
- buff = malloc(sizeof *buff *3);
- if(buff)
- {
- buff[2][3] = 'C';
- printf("(17) %c\n", buff[2][3]);
- free(buff);
- }
- }
- //-----(18)-----
- {
- char** pArr1;
- int** pArr2;
- double** pArr3;
- printf("(18) %zu\n", sizeof *pArr1);
- printf("(18) %zu\n", sizeof *pArr2);
- printf("(18) %zu\n", sizeof *pArr3);
- }
- //-----(19)-----
- {
- char* cpnt;
- int* ipnt;
- float* fpnt;
- double* dpnt;
- printf("(19) %zu\n", sizeof *cpnt);
- printf("(19) %zu\n", sizeof *ipnt);
- printf("(19) %zu\n", sizeof *fpnt);
- printf("(19) %zu\n", sizeof *dpnt);
- }
- //-----(20)-----
- {
- printf("(20) ");
- void(*fncPtrA)() = &testfncA;
- (*fncPtrA)();
- printf("(20) ");
- void(*fncPtrB)(char) = &testfncB;
- (*fncPtrB)('C');
- printf("(20) ");
- void(*fncPtrC)(int, int) = &testfncC;
- (*fncPtrC)(1,2);
- int sum;
- int(*fncPtrD)(int, int) = &testfncD;
- sum = (*fncPtrD)(1,2);
- printf("(20) %d\n", sum);
- int val = 1;
- void(*fncPtrE)(int*) = &testfncE;
- (*fncPtrE)(&val);
- printf("(20) %d\n", val);
- }
- //-----(21)-----
- {
- struct stru {
- int a;
- int b;
- char c;
- float f;
- double d;
- };
- struct stru st;
- st.a = 1;
- st.b = 2;
- st.c = 'C';
- st.f = 0.5f;
- st.d = 1.2;
- printf("(21) %d %d %c %f %f\n", st.a, st.b, st.c, st.f, st.d);
- }
- //-----(22)-----
- {
- typedef struct stru {
- int a;
- int b;
- char c;
- float f;
- double d;
- }Stru;
- Stru st;
- st.a = 1;
- st.b = 2;
- st.c = 'C';
- st.f = 0.5f;
- st.d = 1.2;
- printf("(22) %d %d %c %f %f\n", st.a, st.b, st.c, st.f, st.d);
- }
- //-----(23)-----
- {
- struct stru {
- int a;
- int b;
- char c;
- float f;
- double d;
- };
- struct stru st;
- struct stru* pSt;
- pSt=&st;
- pSt->a = 1;
- pSt->b = 2;
- pSt->c = 'C';
- pSt->f = 0.5f;
- pSt->d = 1.2;
- printf("(23) %d %d %c %f %f\n", st.a, st.b, st.c, st.f, st.d);
- }
- //-----(24)-----
- {
- struct stru {
- int a;
- int b;
- char c;
- float f;
- double d;
- };
- struct stru* st;
- printf("(24) sizeof structure %zu\n", sizeof (struct stru));
- printf("(24) sizeof structure %zu\n", sizeof *st);
- st = malloc(sizeof (struct stru));
- if(st)
- {
- st->a = 1;
- st->b = 2;
- st->c = 'C';
- st->f = 0.5f;
- st->d = 1.2;
- printf("(24) %d %d %c %f %f\n", st->a, st->b, st->c, st->f, st->d);
- free(st);
- }
- }
- //-----(25)-----
- {
- typedef struct stru {
- int a;
- int b;
- char c;
- float f;
- double d;
- }Stru;
- struct stru st;
- struct stru* pst;
- Stru stt;
- Stru* pStt;
- printf("(25) sizeof structure %zu\n", sizeof (struct stru));
- printf("(25) sizeof structure %zu\n", sizeof st);
- printf("(25) sizeof structure %zu\n", sizeof *pst);
- printf("(25) sizeof structure %zu\n", sizeof stt);
- printf("(25) sizeof structure %zu\n", sizeof *pStt);
- }
- //-----(26)-----
- {
- char arr[]={"Zeppelin"};
- int len;
- printf("(26) ");
- len = strlen(arr);
- for(int i = 0; i<len; i++){
- putchar(i[arr]);
- }
- putchar(0x0A);
- }
- //-----(27)-----
- {
- char str[]={"abcdefghij"};
- int len, i;
- char* pnt;
- pnt = str;
- printf("(27) ");
- len = strlen(str);
- for(i = 0; i<len; i++){
- putchar(*pnt);
- pnt++;
- }
- printf("\n(27) ");
- pnt = str + len - 1;
- for(i = 0; i<len; i++){
- putchar(*pnt);
- pnt--;
- }
- char arr[]={'a','b','c','d','e','f','g','h','i','j'};
- int num_of_e = sizeof(arr); //sizeof(char) is 1, sizeof(arr)/sizeof(arr[0]) unnecessary
- printf("\n(27) ");
- pnt = arr;
- for(i = 0; i<num_of_e; i++){
- putchar(*pnt);
- pnt++;
- }
- printf("\n(27) ");
- pnt = arr + num_of_e - 1;
- for(i = 0; i<num_of_e; i++){
- putchar(*pnt);
- pnt--;
- }
- putchar(0x0A);
- }
- //-----(28)-----
- {
- #define MAX_MESSAGE_LENGHT 64
- typedef char Message[MAX_MESSAGE_LENGHT];
- Message messages[3];
- strcpy( messages[0], "(28) first\n" );
- strcpy( messages[1], "(28) second\n" );
- strcpy( messages[2], "(28) third\n" );
- for(unsigned int i = 0; i < sizeof(messages)/sizeof(messages[0]); i++)
- {
- printf( "%s",messages[i] );
- }
- }
- //-----(29)-----
- {
- printf("(29) ");
- int arr[]={0,1,2,3,4,5,6,7,8,9};
- int* pnt1 = malloc(sizeof(arr));
- if(pnt1)
- {
- memcpy(pnt1,arr,sizeof(arr));
- for(unsigned int i=0; i < sizeof(arr)/sizeof(arr[0]); i++)
- {
- printf("%d ", arr[i] );
- }
- free(pnt1);
- }
- else printf("ERROR! memory not allocated.");
- printf("\n");
- }
- //-----(30)-----
- {
- char str[] = "Cassiopeia";
- printf("(30) sizeof str:\t%zu\taddress: %p\n", sizeof str, (void*) str);
- printf("(30) sizeof &str:\t%zu\taddress: %p\n", sizeof &str, (void*) &str);
- printf("(30) sizeof &str[0]:\t%zu\taddress: %p\n", sizeof &str[0], (void*) &str[0]);
- printf("(30) sizeof &str[1]:\t%zu\taddress: %p\n", sizeof &str[1], (void*) &str[1]);
- printf("(30) sizeof &str[2]:\t%zu\taddress: %p\n", sizeof &str[2], (void*) &str[2]);
- int arr[] = {1,2,3,4,5,6,7,8,9,0,1};
- printf("(30) sizeof arr:\t%zu\taddress: %p\n", sizeof arr, (void*) arr);
- printf("(30) sizeof &arr:\t%zu\taddress: %p\n", sizeof &arr, (void*) &arr);
- printf("(30) sizeof &arr[0]:\t%zu\taddress: %p\n", sizeof &arr[0], (void*) &arr[0]);
- printf("(30) sizeof &arr[1]:\t%zu\taddress: %p\n", sizeof &arr[1], (void*) &arr[1]);
- printf("(30) &arr[0] address: %p\n", (void*) &arr[0]);
- printf("(30) &arr[1] address: %p\n", (void*) &arr[1]);
- printf("(30) &arr[2] address: %p\n", (void*) &arr[2]);
- printf("(30) sizeof *arr:\t%zu\n", sizeof *arr);
- printf("(30) sizeof arr[0]:\t%zu\n", sizeof arr[0]);
- }
- //-----(31)-----
- {
- char arr[]={"Zeppelin"};
- char* pnt;
- int len = strlen(arr);
- pnt = arr;
- printf("(31) ");
- while( pnt < &arr[len] )
- {
- putchar(*pnt);
- pnt++;
- }
- putchar('\n');
- }
- //-----(32)-----
- {
- char* strArr[]={"first","second"};
- printf("(32) %s %s\n",strArr[0], strArr[1]);
- swap(&strArr[0], &strArr[1]);
- printf("(32) %s %s\n",strArr[0], strArr[1]);
- }
- //-----(33)-----
- {
- int arr[]={1,2,3};
- int *ptr;
- ptr = &arr[0];
- printf("(33) %d ",*(ptr));
- printf("%d ",*(ptr + 1));
- printf("%d\n",*(ptr + 2));
- }
- //-----(34)-----
- {
- char str[]={"Zeppelin"};
- char *pnt;
- int len = strlen(str);
- printf("(34) ");
- pnt = malloc(len+1);
- if(pnt)
- {
- strcpy(pnt, str);
- for(int i=0; i < len; i++)
- {
- putchar(pnt[i]);
- }
- free(pnt);
- }
- else printf("ERROR! memory not allocated.");
- printf("\n");
- }
- //-----(35)-----
- {
- int arr[]={0,1,2,3,4,5,6,7,8,9};
- int *pnt;
- int size = sizeof(arr);
- int num_of_e = sizeof(arr)/sizeof(arr[0]);
- printf("(35) ");
- pnt = malloc(size);
- if(pnt)
- {
- memcpy(pnt, arr, size);
- for(int i=0; i < num_of_e; i++)
- {
- printf("%d ",pnt[i]);
- }
- free(pnt);
- }
- else printf("ERROR! memory not allocated.");
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment