Skip to content

Commit a5969da

Browse files
author
Senthil Kumaran
committed
Updated C Pointer Examples.
1 parent fc68ad3 commit a5969da

6 files changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
struct tag { /* the structure type */
5+
char lname[20]; /* last name */
6+
char fname[20]; /* first name */
7+
int age; /* age */
8+
float rate; /* e.g. 12.75 per hour */
9+
};
10+
11+
struct tag my_struct; /* define the structure */
12+
void show_name(struct tag *p); /* function prototype */
13+
14+
int main(void) {
15+
struct tag *st_ptr; /* a pointer to a structure */
16+
st_ptr = &my_struct; /* point the pointer to my_struct */
17+
strcpy(my_struct.lname, "Jensen");
18+
strcpy(my_struct.fname, "Ted");
19+
printf("\n%s ", my_struct.fname);
20+
printf("%s\n", my_struct.lname);
21+
my_struct.age = 63;
22+
show_name(st_ptr); /* pass the pointer */
23+
return 0;
24+
}
25+
26+
void show_name(struct tag *p) {
27+
printf("\n%s ", p->fname); /* p points to a structure */
28+
printf("%s ", p->lname);
29+
printf("%d\n", p->age);
30+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
printf("size of a short is %d\\n", sizeof(short));
5+
printf("size of a int is %d\\n", sizeof(int));
6+
printf("size of a long is %d\\n", sizeof(long));
7+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stdio.h>
2+
3+
int main(void) {
4+
5+
int j, k;
6+
int *ptr;
7+
j = 1;
8+
k = 2;
9+
ptr = &k;
10+
printf("\n");
11+
printf("j has the value %d and is stored at %p\n", j, (void *) &j);
12+
printf("k has the value %d and is stored at %p\n", k, (void *) &k);
13+
printf("ptr has the value %p and is stored at %p\n", ptr, (void *) &ptr);
14+
printf("The value of the integer pointed to by ptr is %d\n", *ptr);
15+
16+
return 0;
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <stdio.h>
2+
3+
4+
int main(void) {
5+
6+
int my_array[] = {1, 23, 17, 4, -5, 100};
7+
int *ptr;
8+
int i;
9+
ptr = &my_array[0];
10+
printf("\n\n");
11+
for (i = 0; i < 6; i++) {
12+
printf("my_array[%d] = %d ", i, my_array[i]);
13+
printf("ptr + %d = %d\n", i, *(ptr + i));
14+
}
15+
return 0;
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdio.h>
2+
3+
int main(void) {
4+
5+
char strA[80] = "A string to be used for demonstration purposes";
6+
char strB[80];
7+
8+
char *pA; /* a pointer to type character */
9+
char *pB; /* another pointer to type character */
10+
puts(strA); /* show string A */
11+
pA = strA; /* point pA at string A */
12+
puts(pA); /* show what pA is pointing to */
13+
pB = strB; /* point pB at string B */
14+
putchar('\n'); /* move down one line on the screen */
15+
while (*pA != '\0') /* line A (see text) */
16+
{
17+
*pB++ = *pA++; /* line B (see text) */
18+
}
19+
*pB = '\0'; /* line C (see text) */
20+
puts(strB); /* show strB on screen */
21+
return 0;
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdio.h>
2+
3+
#define ROWS 5
4+
#define COLS 10
5+
6+
int multi[ROWS][COLS];
7+
8+
int main(void) {
9+
int row, col;
10+
for (row = 0; row < ROWS; row++) {
11+
for (col = 0; col < COLS; col++) {
12+
multi[row][col] = row * col;
13+
}
14+
}
15+
16+
for (row = 0; row < ROWS; row++) {
17+
for (col = 0; col < COLS; col++) {
18+
printf("\n%d ", multi[row][col]);
19+
printf("%d ", *(*(multi + row) + col));
20+
}
21+
}
22+
23+
return 0;
24+
}

0 commit comments

Comments
 (0)