c - The variable 'buff' is being used without being defined -


i need read data text file , store in 2d-array. code works good:

#include <string.h>     #include <stdio.h>  int main() {     file *f = fopen("read.txt", "r");     char buff[100][100];     char str[100];     int = 0;     while(fgets(str, 100, f)) {         strcpy(buff[i], str);         i++;     }     return 0; } 

but why doesn't work when try change buff definition in line 5 to:

char (*buff)[100]; 

i expected definition work too. error get:

run-time check failure #3 - variable 'buff' being used without being defined 

char (*buff)[100]; 

here buff pointer array of 100 characters. first should make pointer point valid memory location before storing value in it.

presuming want go dynamic memory allocation can have

char *buff[100]; 

now in fgets() loop allocate memory each pointer individually like

buff[i] = malloc(100);  

note here buff array of 100 char pointers.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -