Sign in to follow this  

Working with an array of strings via Pointers

This topic is 3559 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im trying to figure out what im doing wrong in implementation and I can not find this stuff any were on the net about multi array of strings passed to a function via pointers. The trouble is that it compliles but when I try to read ASCII strings from a file it will not pull the data and I get null strings. I am having an incrediably tough time finding any information for passing an array of strings to a function by a pointer and then working with the array via the pointer. Can some one help me out or point me in the right direction? Oh BTW this in C using dev-C++ on a c project option. Thanks, Thanks, Ok so a bit of an example, #include <stdio.h> #include <stdlib.h> #include <string.h> char readBuffer[20][200]; const char FILENAME[10] = "win.sys"; int grabStringFile(char *wordPlace,FILE *file_fun_ptr); int openFileRead(FILE *file_fun_ptr); main() { int lineNumber = 0; FILE *file_ptr; openFileRead(file_ptr); grabStringFile(&readBuffer_fun_ptr[lineNumber],file_ptr) fclose(file_ptr); return(0); } int grabStringFile(char *wordPlace,FILE *file_fun_ptr) { char line[200]; wordPlace[0]='\0'; testString = fgets(line,sizeof(line),file_fun_ptr); if(testString == NULL) { return(201); } line[strlen(line)-1] = '\0'; strcpy(wordPlace,line); return(0); } int openFileRead(FILE *file_fun_ptr) { file_fun_ptr = fopen(FILENAME, "a"); if (file_fun_ptr == NULL) { return(0); } else { return(1); } }

Share this post


Link to post
Share on other sites
Pointers are passed to functions as copies. As such, "int openFileRead(FILE *file_fun_ptr);" cannot modify the FILE pointer in main. You would need to pass a pointer to the FILE pointer (ie. a pointer-to-a-pointer) to do this. For an example of why changing a pointer will not work, see here.

That said, given that openFileRead is an extremely thin wrapper over fopen() anyway, it would be easier to put the relevant code inside main. What is more important here is that we actually handle the "couldn't open file" error. Your openFileRead function returns an integer indicating whether the operation was successful, but you never checked the return value inside main.


// always fully declare your function, with return types
int main()
{
int lineNumber = 0;
FILE *file_ptr;

file_ptr = fopen(FILENAME,"a");
if(!file_ptr)
{
perror(FILENAME);
return 1;
}

grabStringFile(&readBuffer_fun_ptr[lineNumber],file_ptr)
fclose(file_ptr);

// return isn't a function call, don't bother putting parentheses around
// the value you are returning
return 0;
}



There are a few other problems in your code, but this might give you enough to figure them out on your own.

Share this post


Link to post
Share on other sites

This topic is 3559 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

Sign in to follow this