Jump to content

array de strings porque o ponteiro nao aponta??


suzy
 Share

Recommended Posts

Tenho o seguinte codigo



#include <string.h>

main()
{
FILE *fp;
char str[50],**mat;
fp=fopen("lol.txt","r");
int i,z=0;
if(fp==NULL)
{
printf(" erro\n");
}


mat=(char**)malloc(4*sizeof(char*));
               for(i=0;i<50;i++){
                   mat[i]=(char*)malloc(sizeof(char));
                   }
			   
while(fscanf(fp,"%[^\n]",str)==1)
               {
               printf(" %s\n",str);
		   mat[z++]=str;
		   printf(" mat e %d\n",mat[z++]);
		   }
fclose(fp);

fflush(stdin);
getchar();
}

basicamente, o que eu quero e ler de um ficheiro as strings e colocar um vetor de ponteiros a apontar para cada string.
tipo:
p[0]="portugal"
p[1]="programar"

acontece que neste codigo as strings nao "entram" no ponteiro, porque?





Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.