-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellmemory.c
More file actions
52 lines (42 loc) · 1.01 KB
/
shellmemory.c
File metadata and controls
52 lines (42 loc) · 1.01 KB
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
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct MEM{
char*var;
char *value;
} arr;
arr arrayMem[100];
int count=0;
int add(char array[] , char value[]){
// put element when array is empty
if (arrayMem[0].var == NULL){
arrayMem[0].var = strdup(array);
arrayMem[0].value= strdup(value);
count ++;
} else {
//detect duplicate element
for (int i = 0; i < count;i++){
if (strcmp(arrayMem[i].var,array) == 0){
arrayMem[i].value = strdup(value);
return 0;
}
}
// insert element when not duplicate and when not empty array
arrayMem[count].var = strdup(array);
arrayMem[count].value= strdup(value);
count++;
}
return 0;
}
int printList(char var[]){
// print element that exists in the memory
for (int i=0; i <count;i++ ){
if (strcmp(arrayMem[i].var,var) == 0){
printf(" %s\n",arrayMem[i].value);
return 0 ;
}
}
// variable doesnt exist in the memory
printf("Variable does not exist\n");
return 0;
}