Retrieving a contain of an Array using pointer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maxou2013
    New Member
    • Nov 2013
    • 3

    #1

    Retrieving a contain of an Array using pointer

    Hi,

    I was struggling the last 2 days with this C script supposing to open a list of strings (input as fopen(argv[1])and allowing to access to any element of the list. I created an array *gcm[10000] and a pointer *(*gcm_ptr[10000] = &gcm. However, when I try to list whatever n[i], it always gives me the last entry. Thanks in advance for any help.

    Max
  • maxou2013
    New Member
    • Nov 2013
    • 3

    #2
    HERE IS THE SOURCE CODE I USED.

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

    #define NJFD 3059

    main(int argc, char **argv){

    FILE *fict;

    unsigned short int *data_tab;

    char line[1024];
    char *cp;
    char *n[10000];
    char *(*n_ptr)[10000] = &n;

    int m,i,quit;




    /****** Open files and initialize ******/
    if (argc != 2) {
    printf("\n command line: <List File> \n");
    exit(1);
    }

    if ( (fict = fopen(argv[1],"r")) == NULL ) {
    printf("\n Cannot OPEN TAB file 1\n");
    exit(1);
    }


    data_tab = (unsigned short int *)calloc(NJFD, sizeof(unsigned short int));
    if (data_tab == NULL) {
    printf("\n Not enough memory for tab data short int array \n");
    exit(1);
    }

    m = 0;
    quit = 0;
    if(fict != NULL)
    {
    while((fgets(li ne, 1024, fict)) && (quit == 0))
    {
    cp = line;
    while((*cp != '\0') && (quit == 0))
    {
    if((m < 10000))
    {
    n[m] = cp;
    while((*cp != ',') && (*cp != '\0')){
    cp++;
    }
    if(*cp == ',') cp++;

    } /* end if */
    else
    {
    quit = 1;
    fprintf(stderr, "Error: Rows has exceeded 10000. Exiting program.\n");
    } /*end else */

    } /* end while ((*cp */
    m++;
    } /* end while ((fgets */

    fclose(fict);

    } /*end if (fict */
    else
    {
    fprintf(stderr, "Error: Unable to open textfile.\n");
    }

    for (i = 0; i < m; i++) printf("List %d: %s\n",i+1, (*n_ptr)[i]);

    if (data_tab != NULL) free(data_tab);

    }

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      *gcm[10000] and a pointer *(*gcm_ptr[10000] = &gcm.

      gcm is an array of 1000 pointers.
      gcm_ptr is a pointer to an array of 1000 pointers.

      Where are the strings exactly?

      Comment

      • maxou2013
        New Member
        • Nov 2013
        • 3

        #4
        The strings are input from the (fict = fopen(argv[1],"r"); please, see the script above

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          fict is a FILE pointer. A FILE is not an array of strings

          You have to read the string into an array like line[1024].

          Then you need to allocate a new array of 1024 and copy line into the new array. Then you can assign the address of the new array to n[m].

          The way it is now, line is assigned to cp but this only assigns the address of line[0]. Since line is a local variable the address of line[0] never changes and so cp never changes either. The array n elements are all the same address. The address of line[0].

          Usually, malloc is used to allocate a new array of 1024 before each read. The address of the array would go into n[m]. Then you would read directly into n[m].

          Comment

          Working...