-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_pointer_array.c
118 lines (97 loc) · 4.17 KB
/
example_pointer_array.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <stdio.h>
#include <stdlib.h>
/*
https://www.infoq.com/news/2020/08/c2rust-transpiler
C type systems hide so much relevant information required to make even basic assumptions.
For example, if we take strncpy declaration, it really requires its two arguments destination and source to be arrays,
but its signature hides this fact behind the generic notion of a pointer to a character:
char* strncpy (char* destination, const char* source, size_t num);
Even though some languages are context-sensitive, context-sensitive grammars are rarely used for describing computer languages.
For instance, C is slightly context-sensitive because of the way it handles identifiers and type,
but this context-sensitivity is resolved by a special convention, rather than by introducing context-sensitivity into the grammar.
http://matt.might.net/articles/grammars-bnf-ebnf
*/
//characters
void functionxyz_char(char** outVariable)
{
*outVariable = malloc( 2 * sizeof(char) );
(*outVariable)[0] = 'A';
(*outVariable)[1] = 'B';
printf("Inside functionxyz_char \n");
printf("theVariablechar[0] = %c \n",(*outVariable)[0]);
printf("theVariablechar[1] = %c \n",(*outVariable)[1]);
printf("Address[0] = %p \n", &( (*outVariable)[0] ) );
printf("Address[1] = %p \n", &( (*outVariable)[1] ) );
}
//strings
void functionxyz_string(char*** outVariable)
{
*outVariable = malloc( 2 * sizeof(char*) );
(*outVariable)[0] = "A";
(*outVariable)[1] = "B";
printf("Inside functionxyz_string \n");
printf("theVariablestring[0] = %s \n",(*outVariable)[0]);
printf("theVariablestring[1] = %s \n",(*outVariable)[1]);
printf("Address[0] = %p \n", &((*outVariable)[0]));
printf("Address[1] = %p \n", &((*outVariable)[1]));
}
int main() {
/*
foo[x] = *(foo + x)
*(foo[x]) = *( *(foo + x) )
(*foo)[x] = *( *foo + x )
if you do *outVariable[0] it works necause *outVariable[0] is basically **outVariable
So &theVariable[1] is & (theVariable[1]) and not (&theVariable)[1]
variable char = character value => char x = 'A';
variable char* = character pointer / character array => char *x = { 'A', 'B', 'C', 0 };
function argument char** = reference value to character array
variable char* = string => char *y = "ABC";
variable char** = string pointer / string array => char **y = {"ABC", "DEF", "GHI", 123};
function argument char*** = reference value to string array
char** reference to character array
char*** reference to string array
The last digit is number of items
const char *a[2];
a[0] = "blah";
a[1] = "hmm";
Here the last digit become number of character for each item
char a[2][14];
strcpy(a[0], "blah");
strcpy(a[1], "hmm");
*/
char* theVariablechar = NULL;
functionxyz_char(&theVariablechar);
printf("after function return, Address[0] = %p \n", &theVariablechar[0]);
printf("after function return, Address[1] = %p \n", &theVariablechar[1]);
printf("theVariablechar[0] = %c \n",theVariablechar[0]);
printf("theVariablechar[1] = %c \n",theVariablechar[1]);
printf("\n --------- \n\n");
char** theVariablestring = NULL;
functionxyz_string(&theVariablestring);
printf("after function return, Address[0] = %p \n", &theVariablestring[0]);
printf("after function return, Address[1] = %p \n", &theVariablestring[1]);
printf("theVariablestring[0] = %s \n",theVariablestring[0]);
printf("theVariablestring[1] = %s \n",theVariablestring[1]);
return 0;
}
/*
Inside functionxyz_char
theVariablechar[0] = A
theVariablechar[1] = B
Address[0] = 0x5634e70382a0
Address[1] = 0x5634e70382a1
after function return, Address[0] = 0x5634e70382a0
after function return, Address[1] = 0x5634e70382a1
theVariablechar[0] = A
theVariablechar[1] = B
---------
Inside functionxyz_string
theVariablestring[0] = A
theVariablestring[1] = B
Address[0] = 0x5634e70386d0
Address[1] = 0x5634e70386d8
after function return, Address[0] = 0x5634e70386d0
after function return, Address[1] = 0x5634e70386d8
theVariablestring[0] = A
theVariablestring[1] = B
*/