1

I've got a number of files that I am reading the contents into a char array, I would like to store the contents I read from each file in an array of characters so that I could access the separate character arrays (file contents) by index, I am not sure of the syntax on how to do this.

My understanding is that I need to set each of the memory addresses in my patterns array to one memory address per char array; to the char array stored in patternData but I do not know how to set this. What is the syntax that I am missing that I need to get this to happen?

What I've tried

I would have thought that If I was storing a type of char* then I would need a type char** to store the separate arrays of char arrays.

I would access a char* by using the following notation to set the memory address of the pattern index to

&patterns[INDEX] = &pData;

However this does not work. There is a plethora of "char pointer array" questions but I'm not sure of the correct way to do this simple assignment of pData to an index of patterns.

char *tData;
int tLength;
char *pData;
int pLength;

char **patterns;

void ReadFromFile(FILE *f, char **data, int *length) //This is what is passed into function


    int main(int argc, char **argv)
    {
        for (int i = 1; i <= 5; i++)
        {
            FILE *f;
            char fileName[1000];
            sprintf(fileName, "file%d.txt", i);
            f = fopen(fileName, "r");
            if (f == NULL)
                return 0;

            ReadFromFile(f, &pData, &pLength); //works to here, contents of file assigned to pData
            fclose(f);

            &patterns[i - 1] = &pData;

        }

        return 0;

    }
1
  • Shouldn't it read patterns[i - 1] = pData;? Commented Mar 18, 2015 at 15:39

2 Answers 2

2

This line is incorrect:

&patterns[i - 1] = &pData;

You are trying to assign to the result of the "take an address" operator, which is not possible because it's not an lvalue.

The assignment should be as follows:

patterns[i - 1] = pData;

but you need to allocate patterns before you do this. You can use

patterns = malloc(sizeof(char*)*5);

or simply declare patterns to be an array of five:

char *patterns[5];

This assumes that your ReadFromFile function allocates a char*, and assigns it to the address pointed to by pData. Note that you need to free all pointers that were obtained through malloc/calloc/realloc.

Sign up to request clarification or add additional context in comments.

Comments

0

I haven't read particularly carefully, but it strikes me as odd that you're taking the reference of patterns when it's a **. This is in the line:

&patterns[i-1] = &pData;

1 Comment

Sorry I think thats a line written with inherent confusion, I basically need to assign the memory address at that patterns index to the address of pData

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.