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 6889143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:07:07+00:00 2026-05-27T06:07:07+00:00

I am doing a book exercise (not homework, since i’m self-learning) in which I’m

  • 0

I am doing a book exercise (not homework, since i’m self-learning) in which I’m supposed to write a telephone book using an array of type struct.
So I defined:

typedef struct contact {

    char cFirstName[10];
    char cLastName[10];
    char iTelphone[12];

} address ; // end type

Next, I defined:

address myContacts[5] = { 
                        { 
                { '\0' }  // inner most braces, 
                          // tell gcc to put 0 in all
                      // members of each struct ?
                }  // first braces, tell gcc we have a 
                   // a struct as array member
                }; // outer most braces, tell gcc we have 
                   // an array

Now I have a function which prints the array content.
However, I don’t want to print all the array, because I’m only interested
in array elements who’s members are not empty. So I tried the following:

void printContacts( address * myContacts ){
    printf("Inside printContacts");
    int i = 0;

    while ( i < 5 ) {
        if (myContacts[i].cFirstName == NULL )
            printf("%s", myContacts[i].cFirstName); //does not work

        if (myContacts[i].cFirstName == '\0' )
            printf("%s", myContacts[i].cFirstName); //does not work

        if (myContacts[i].cFirstName[0] != 0 )
        {
            printf("%s", myContacts[i].cFirstName ); //does work!   
        }
        i++;
    }
}

So, my questions are:

  1. Am I initializing the array elements members to be really Null (e.g cFirstName[10]) ?
  2. Is there a better way ?
  3. How can I check that an array element member is empty ?

Thanks in advance for your answers!

  • 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-27T06:07:07+00:00Added an answer on May 27, 2026 at 6:07 am

    So, my questions are:

    Am I initializing the array elements members to be really null (e.g cFirstName[10]) ?
    Is there a better way ?
    How can I check that an array element member is empty ?
    

    Thanks in advance for your answers!

    What you have done in initializing your struct array could be improved upon.

    The first brace says, “initialize the array with my contents:”

    address myContacts[5] = { … }

    The second set of braces says, “initialize the first struct in the array with the following:”

    address myContacts[5] = { 
        { ... } ,   // each of these braces initializes a different struct in the 
        { ... } ,   // array (there are five inner brace sets as there are five  
        { ... } ,   // structs in the array).
        { ... } ,
        { ... }
     }
    

    Note that it is good form to initialize all of your array elements at once, or to use
    a for loop to do so. This is why your original initialization confuses me; I’m not sure it is “correct”, even if it runs.

    Then, you put more braces inside of those (if you still want to do brace initialization for this application) to initialize the arrays in the struct:

    address myContacts[5] = { 
        {{'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'},
         {'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'},
         {'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0, '\0', '\0'} },
         .
         .
         .
     }
    

    That is the way to “zero out” your array first using bracket notation. Not too many people do it; it gets tedious. Usually, for a real application people would use the heap (memory that you can allocate on-the-fly, as shown below) for something like this, in which case you could simply use pointers in your struct:

    typedef struct contact {
    
        char *cFirstName;
        char *cLastName;
        char *iTelphone;
    
    } address ; // end type
    

    You would then allocate a contacts list:

    address *myContacts = (address*) malloc(sizeof(address)*5); 
                          // the above allocates a five-element array
    int i;
    for (i = 0; i < 5; i++)
    {
        myContacts[i].cFirstName = NULL;
        myContacts[i].cLastName = NULL;
        myContacts[i].iTelephone = NULL;
    }
    

    allocate strings when you need to add to the address book:

    myContacts[0].cFirstName = (char*) malloc(sizeof(char)*20);
    strcpy(myContacts[0].cFirstName,"Tom, Dick, or Harry");
    

    and when you’re done, free everything you’ve allocated:

    int i;
    for (i = 0; i < 5; i++)
    {
        if (myContacts[i].cFirstName != NULL) // since we initialized all pointers 
        {                                     // to null, this only isn't null when
            free(myContacts[i].cFirstName);   // it's been previously allocated
        }
        if (myContacts[i].cLastName != NULL)
        {                                    
            free(myContacts[i].cLastName);   
        }
        if (myContacts[i].iTelephone != NULL) 
        {                                     
            free(myContacts[i].iTelephone);   
        }
    }
    

    This allows you to create variable-length strings, arbitrarily large contact lists, and if you want to check to see if an entry is empty, simply check to see if it’s pointer is null:

    if (myContacts[i].cFirstName != NULL)
    {
        printf("%s", myContacts[i].cFirstName ); //does work!
    }
    

    I hope this concept at least proves useful to you, even if you don’t end up using malloc (there are good reasons for or against using it in every program). 🙂

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

Sidebar

Related Questions

while doing some homework in my very strange C++ book, which I've been told
I am learning templates. Which book is worth buying for doing template programming? I
Currently I'm learning C++ in my book and they had an exercise on using
I'm doing a book exercise that says to write a program that generates psuedorandom
As I'm learning the iPhone API, the book I'm using has me doing everything
I was doing an exercise on F# Wiki Book on List (scroll to the
I am learning Objective-C using Stephen Kochan's excellent book Programming in Objective-C 2.0. I
I am currently doing a task in a book which asks me to calculate
I am a beginner in Java and am doing an exercise from a book.
I am doing a homework assignment that reads in a book. First, a line

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.