-1

I'm really new to programming with C and have encountered the following problem:

When reading about pointers at https://www.tutorialspoint.com/cprogramming/c_pointer_to_an_array.htm, They have the following example:

#include <stdio.h>

int main () {

   /* an array with 5 elements */
   double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
   double *p;
   int i;

   p = balance;
 
   /* output each array element's value */
   printf( "Array values using pointer\n");
    
   for ( i = 0; i < 5; i++ ) {
      printf("*(p + %d) : %f\n",  i, *(p + i) );
   }

   printf( "Array values using balance as address\n");
    
   for ( i = 0; i < 5; i++ ) {
      printf("*(balance + %d) : %f\n",  i, *(balance + i) );
   }
 
   return 0;
}

This example works fine, but when I try to replicate it in my code with a similar situation I'm getting an error.

For example, I create and fill an array with values from a text file. This code works fine and outputs each string in the array as I would expect when finished

#include <stdio.h>
#include <string.h>

#define MAXCHAR 10000

int main() {
    
    FILE *fp;
    char* filename = "../MagicProg/Files/MagicProg_csv_ikoria.csv";
    char str[MAXCHAR];

    char c;
    
    char seps[] = "|";
    char *token;

    int total_line_count = 0;

    fp = fopen(filename, "r");
    
    if (fp == NULL)
    {
        printf("Could not open file %s", filename);
        return 1;
    }
    
    for (c = getc(fp); c != EOF; c = getc(fp)) 
    {
        if (c == '\n') 
        {
            total_line_count += 1; 
        }
    }   

    fclose(fp);
    fp = fopen(filename, "r");

    // declare the arrays
    char csv_card_names[total_line_count][30];
    
    int curr_line_index = 0;
    int line_token_count = 0;

    while (fgets(str, MAXCHAR, fp) != NULL)
    {
 
        line_token_count = 0;
        token = strtok( str, seps );
            
        while ( token != NULL )
        {
            if ( curr_line_index > 0 ) 
            {
                if ( line_token_count == 0 ) 
                {
                
                    strcpy(csv_card_names[curr_line_index - 1], token);
                }
            }
            
            line_token_count += 1;
            token = strtok( NULL, seps );
        }

        curr_line_index += 1;
    }
     
    for ( int i = 0; i < total_line_count; i++ ) {
        printf("LINE=\n");
        printf("%s\n", csv_card_names[i]);
        
    }
        
    fclose(fp);
    return 0;
    
    
}

However, In trying to understand arrays I'm trying to re-write the last block of the code where I output the array elements using pointers. From the example above, I try creating a pointer and assigning the array to it just before looping through total line count at the end

...

char *p[30];
p = csv_card_names;
 
for ( int i = 0; i < total_line_count; i++ ) {
    printf("LINE=\n");
    printf("%s\n", csv_card_names[i]);       
}

But Geany is giving me the error "assignment to expression with array type". I'm not sure how what I'm doing is different than the example, where they assign:

p = balance;

The array (i think) has values of strings equalling 30 chars, so I'm trying to create a point capable of referencing those but am getting this error. any help is appreciated thanks

3
  • char *p[30] -- is an array-of-pointers char [30] (e.g. 30 pointers to char) While char (*p)[30] -- is a pointer-to-array of char[30] (e.g. a pointer to a character array of 30 chars) As to why the parens are needed, see C Operator Precedence Commented Jun 28, 2020 at 23:54
  • A few links that provide basic discussions of pointers may help. Difference between char pp and (char) p? and Pointer to pointer of structs indexing out of bounds(?)... Commented Jun 29, 2020 at 0:00
  • Tip: Instead of *(x + y) just use x[y]. Not only is it less typing but it makes way more sense to anyone familiar with C. Remember you can do x[y][z] and even deeper nested structures, which just will not fly as computing those offsets is really not easy. Commented Jun 29, 2020 at 0:55

1 Answer 1

3

char *p[30] is not a pointer - it is an array of pointers. As any other array it cannot be assigned.

you need to declare it as pointer to array of 30 chars.

char (*p)[30] = ....

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

1 Comment

It's worth noting that char* is often better than char (*p)[30] because the length is largely irrelevant when dealing with pointers and needs to be specified in a more concrete way, like as an additional function argument.

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.