Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3231374
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:02:17+00:00 2026-05-17T17:02:17+00:00

I am attempting to read a file with some random names in the format

  • 0

I am attempting to read a file with some random names in the format “ADAM”,”MARK”,”JESSIE” …..

I have some constraints, the file should be read in a function but should be accessible form the main function with no global variables.
The file size and the no of names in the file are not known.
This is what I have done till now. I have some difficulty with dynamic 2d array as I have not used them much.

/* Function to read from the file */
int read_names(FILE *input, char ***names, int *name_count)
{
    int f1,size,count,i,j=0;
    char **name_array,*text,pos=0;
    /* get the file size */
    f1=open("names.txt",O_RDONLY);
    size=lseek(f1,0,SEEK_END);
    close(f1);
    /* Reading all the characters of the file into memory */
    //Since file size is known we can use block transfer
    text=(char *) malloc(size * sizeof(char) );
    fscanf(input,"%s",text);

    /* Finding the no of names in the file */
    for(i=0;i<size;i++)
    {
        if(text[i]==',')
            count++;
    }
    printf("No. of names determined\n");

    /* Assigning the Name count to the pointer */
    name_count=(int*)malloc(sizeof(int));
    *name_count=count;


    name_array=(char **) malloc(count * sizeof(char *));
    for(i=0;i<count;i++)
    {
        name_array[i]=(char*) malloc(10 *sizeof(char ));
    }
    for(i=0;i<size;i++)
    {
        if(text[i]!='"')
            if(text[i]==',')
            {
                **name_array[pos][j]='\0'; //error here
                pos++;
                j=0;
            }
            else
                name_array[pos][j++]=text[i];
    }
    printf("Names Counted\n");
    printf("Total no of names: %d\n",*name_count);
    names=(char ***) malloc(sizeof(char **);
    names=&name_array;
    return 1;
}

/* Main Function */
int main(int argc, char *argv[])
{
    FILE *fp;
    char ***names;
    int *name_count;
    int status;
    // Opening the file
    fp = fopen("names.txt","r");
    // Now read from file
    status = read_names(fp,names,name_count);
    printf("From Main\n");
    fclose(fp);
    system("PAUSE");
    return 0;
}

I use WxDev and am getting an error “invalid type argument of `unary *’ when I try to assign the null character.

Any pointers on how to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:02:17+00:00Added an answer on May 17, 2026 at 5:02 pm
                **name_array[pos][j]='\0'; \\error here
    

    I see that name_array is declared as

    char **name_array
    

    Problem is that

    **name_array[pos][j] tries to dereference (twice!!) a character

    **   (name_array[pos]) [j];    /* name_array[pos] is of type (char*) */
    ** ( (name_array[pos]) [j] );  /* name_array[pos][j] is of type (char) */
    

    You cannot dereference a character.

    Suggestion: simplify your code.
    When code doesn’t “behave”, it usually is not because it is too simple 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been attempting to write some routines to read RSS and ATOM feeds
I have a Excel .CSV file I'm attempting to read in with DictReader. All
I am attempting to write some code to read in a *.CSPROJ file using
I'm currently attempting to read information about an object from the web.config file and
I am attempting to pull some information from my tnsnames file using regex. I
I am attempting to read a large XML document and I wanted to do
I'm attempting to use TinyXML to read and save from memory, instead of only
Does attempting to develop some sort of game, even just as a hobby during
I am attempting to include a header in my html file. <!--#include virtual=/header.shtml -->
I'm attempting a simple test of binary file I/O using the STL copy algorithm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.