0

I want to store each line of the file in an 2D array, and anthor array pointing each line (so I can identify each line), I need to pass this pointers array to a function so I can manipulate my lines, I d'ont know how to do that

I have this code to read and store in the arrays

char ligne[MAX];
//open end test the file
FILE* fichier = fopen("csp.txt","r");
if(fichier == NULL){
    printf("can't open the file \n");
    return EXIT_FAILURE;
}
//the first line in the file contain number of other lines
fgets(ligne, sizeof(ligne), fichier);
int nbrTaille = strtol(ligne, NULL, 10);

//array of pointers
char (*tab)[nbrTaille] = malloc(nbrTaille * sizeof(ligne));
int i = 0;

//tab array point each line
while(fgets(ligne, sizeof(ligne), fichier)){

    if(ligne == NULL) EXIT_FAILURE;

    strncpy(tab[i], ligne, strlen(ligne));  
    printf("line%d : %s\n", i, tab[i]);
    i++;
}
//call the funnction by passing array of pointers and the number of lines
allDiff(tab, nbrTaille);

the file I'm reading is

2
1 2
2 3

to receive the array by the function I tried this but it doesn't work

void allDiff(char** T, int taille)
2
  • Your comment //array of pointers is not accurate. That doesn't declare an array of pointers; it declares a pointer to an array. There is a difference. That decl should be char (*tab)[MAX] = .... If you want a dynamic allocation for each string then all of that needs to change. And unrelated, if(ligne == NULL) EXIT_FAILURE is worthless. ligne is an automatic array and will thus never be NULL. Commented Dec 11, 2016 at 20:12
  • can I have this type of array (example: [1][1,2], [2][2,3]) without dynamic allocation (so that each line "i" contains as columns "j" my file lines [ i ][ j ]) ?? Commented Dec 11, 2016 at 20:18

3 Answers 3

1

I am always confused with the parenthesies and asterisks. The easier way is to declare a double pointer:

char ** tab;

//you need to take care of errors. I am not doing it for simplicity
//tab = {pointer1, pointer2, pointer3, ...., pointerN} total memory needed N * sizeof(pointer)
tab = malloc(lines* sizeof(tab)); //how many pointers you want
for(i = 0; i < lines; i++){
    tab[i] = malloc(MAX); //each string
}

In the end free the memory:

for(i = 0; i < lines; i++){
    free(tab[i]);
}
free(tab);

EDIT complete code

char ligne[MAX];
//open end test the file
FILE* fichier = fopen("csp.txt","r");
if(fichier == NULL){
    printf("can't open the file \n");
    return EXIT_FAILURE;
}
//the first line in the file contain number of other lines
fgets(ligne, sizeof(ligne), fichier);
int nbrTaille = strtol(ligne, NULL, 10);

//array of pointers
//char (*tab)[nbrTaille] = malloc(nbrTaille * sizeof(ligne));
int i = 0;
char **tab;

tab = malloc(nbrTaille * sizeof(tab)); //how many pointers you want
for(i = 0; i < nbrTaille; i++){
    //sizeof(ligne) is equal to MAX
    tab[i] = malloc(MAX); //each string
}

i = 0;

//tab array point each line
while(fgets(ligne, sizeof(ligne), fichier)){

    if(ligne == NULL) EXIT_FAILURE;

    strncpy(tab[i], ligne, strlen(ligne));  
    printf("line%d : %s\n", i, tab[i]);
    i++;
}
//call the funnction by passing array of pointers and the number of lines
allDiff(tab, nbrTaille);
Sign up to request clarification or add additional context in comments.

10 Comments

sizeof(int) is wrong, and will invoke UB on any platform where int and char* are not the same size. Use sizeof *tab instead.
@WhozCraig Yes. I forgot that pointer size is different from int.
I'm filling my table by reading lines file, I can't fill it like that it's not sizeof(int) but sizeof(line)
@devhicham I thought nbrTaille was the number of lines. Change it to lines instead.
how can I declare tab with sizeof(tab) in the malloc function, is that correct ? and what is MAX ? I cant fill it like this because I need to point it to the line when reading it, I don't understand what you mean
|
1

void function(char tab[][MAXLEN], ...); This will do.

2 Comments

I can't cause the length is calculated inside the code, how to pass it ?
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
0

For the sake of readbility and sanity, typedef C function pointers before creating arrays or double pointers to them.

/* typedef the function pointer. An ftabptr points to a void function that
   takes a char * */
typedef void (*ftabptr)(char *p);


/* create an uninitalised list of twenty of them */
int N = 20;
ftabptr *pointerlist = malloc(N * sizeof(ftabptr));

However I don't think you really want to do this. You can write a functioning program that does weird things with tables of function pointers, but normally you use a language other than C if you want to play that game (Lisp-like languages, etc). The high level language then often emits C as an intermediate step.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.