How to fill-up an pointer to an array of char in C
I'm writing a little code, nothing fancy, which gets the open file from /proc/<pid>/maps:
Code:
#include <stdio.h> Never used a proper char array, so really not sure how to. Can anyone show me how or point me in the right direction? when I've tried my luck, I've used something like: Code:
char **s_array; |
You need an array of pointers to arrays of characters (C strings). The outer array is of unknown length, so you can either read the file twice: once to determine the number of lines and then to read each line, or you can create an array that is "big enough"
Code:
char **s_array = calloc(4096, sizeof(char *)); // enough for 4096 strings Code:
size_t len = strlen(pos); |
Sorry, but in calloc isn't the fist argument no_of_elements? Or am I reading wrong the documentation?
|
Never mind it simply takes the product of the two values
Also sizeof(char)==1. Plus there is a handy function called 'strdup' |
Thanks guys, all worked.
|
All times are GMT -5. The time now is 09:49 PM. |