-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabappend.c
50 lines (32 loc) · 981 Bytes
/
abappend.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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define NULL_CHECK(x) do{\
if (!x) {\
fprintf(stderr, "Failed to allocate memory for pointer %s inside %s", #x, __func__); \
exit(EXIT_FAILURE); \
} \
} while (0)
struct gbuf {
char* buff;
int len;
};
void gbufAppend(struct gbuf* gbuf_ptr, const char* s, int len)
{
gbuf_ptr->buff = realloc(gbuf_ptr->buff, gbuf_ptr->len + len) ; // Give the buff member of gbuf1 instance larger memoryblock
NULL_CHECK(gbuf_ptr->buff); //Check for realloc failure
memcpy(gbuf_ptr->buff + gbuf_ptr->len, s, len); // Copy s in the buffer
gbuf_ptr->len += len; //Update the lenght of the new buffer
//printf(" Buff = \"%s\" \n", gbuf_ptr->buff);
//printf(" len = %d \n", gbuf_ptr->len);
}
int main()
{
struct gbuf gbuf1 = {NULL, 0};
gbufAppend(&gbuf1, "Hello ", 6);
printf("\n %s \n", gbuf2.buff);
gbufAppend(&gbuf1, "World\0", 6);
printf("\n %s \n", gbuf1.buff);
// It works !
return 0;
}