initial import
[staff/goc4/2018bti7061.git] / c-4-pointers / examples / c-pointers / pointers7.c
1 #include <stdio.h>
2 #include <string.h>
3
4 main(){
5 char line[80];
6 char copy1[80];
7 char copy2[80];
8 char copy3[80];
9 char copy4[80];
10 char copy5[80];
11
12 puts("Enter line");
13 scanf(" %[^\n]",line);
14
15 /* Function for reading the length of a string */
16 int length = strlen(line);
17
18 int i;
19 /* We add one to copy the '\0' */
20 for(i=0;i<length+1 ; i++){
21 copy1[i]=line[i];
22
23 }
24
25 for(i=0;i<length+1 ; i++){
26 copy2[i]=*(line+i);
27
28 }
29
30
31 for(i=0;i<length+1 ; i++){
32 *(copy3 + i)=*(line + i);
33
34 }
35
36
37 for(i=0;i<length+1 ; i++){
38 *(copy4 + i)=line[i];
39
40 }
41
42 /* substring, without copy, same memory part */
43 char* p1 = &line[2];
44 char* p2 = line+2;
45
46 printf("line = %s\n",line);
47 printf("Copy1 = %s\n",copy1);
48 printf("Copy2 = %s\n",copy2);
49 printf("Copy3 = %s\n",copy3);
50 printf("Copy4 = %s\n",copy4);
51 printf("p1 = %s\n",p1);
52 printf("p2 = %s\n",p2);
53
54 }
55 /* Ouptput
56 bie1@VirtualBox:~/pointers$ ./pointers7
57 Enter line
58 Emmanuel Benoist
59 line = Emmanuel Benoist
60 Copy1 = Emmanuel Benoist
61 Copy2 = Emmanuel Benoist
62 Copy3 = Emmanuel Benoist
63 Copy4 = Emmanuel Benoist
64 p1 = manuel Benoist
65 p2 = manuel Benoist
66
67 */