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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:21:04+00:00 2026-06-15T10:21:04+00:00

I have a nested array of structures in the following form: #include <stdio.h> #include

  • 0

I have a nested array of structures in the following form:

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

typedef struct
{
    char* e_name;
    char* e_lastname;
}emp_name;

typedef struct
{
   emp_name name;
   int id;
}emp;

int main(int argc, char *argv[])
{
  int i;
  int cod=100;
  emp job[3];
  for (i=0;i<3;i++)
  {

     scanf("%s",&job[i].emp.e_lastname);
     job[i].id=cod;
      cod++;
 }
     for (i=0;i<3;i++)
  {
       printf("%s",job[i].emp.e_lastname);
         printf("%d\n",job[i].id);
  }
 system("PAUSE");   
    return 0;
  }

but the program hangs in the printing part, why is that?
Thanks

  • 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-06-15T10:21:04+00:00Added an answer on June 15, 2026 at 10:21 am

    You have three problem:

    First you want to access:

    job[i].name.e_lastname
    

    not

    job[i].emp.e_lastname
    

    Second You should have:

    scanf("%s",&job[i].name.e_lastname); 
    

    instead of

    scanf("%s",job[i].name.e_lastname);
    

    You do not pass & since it is an array you are passing to the scanf function.

    Third problem you should allocate memory to your char *e_lastname and char *e_name camps of the struct emp_name.

    Note that:

    scanf

    int scanf ( const char * format, … );

    Reads data from stdin and stores them according to the parameter
    format into the locations pointed by the additional arguments.

    The additional arguments should point to already allocated objects
    of the type specified by their corresponding format specifier within
    the format string. (source)

    So you want this:

    int main(int argc, char *argv[])
    {
      int i;
      int string_size = 10;
      int cod=100;
      emp job[3];
    
    
      for (i=0;i<3;i++) // Allocate space for the string you will access.
      {
      job[i].name.e_name = malloc(sizeof(char)*string_size);
      job[i].name.e_lastname = malloc(sizeof(char)*string_size);
      }
    
      for (i=0;i<3;i++)
      {
    
         scanf("%s",job[i].name.e_lastname);
         job[i].id=cod;
         cod++;
     }
         for (i=0;i<3;i++)
      {
           printf("%s",job[i].name.e_lastname);
             printf("%d\n",job[i].id);
      }
     system("PAUSE");   
        return 0;
      }
    

    Consider the fact that using scanf is unsafe, because:

    If you use the %s and %[ conversions improperly, then the number of
    characters read is limited only by where the next whitespace character
    appears. This almost cetainly means that invalid input could make your
    program crash, because input too long would overflow whatever buffer
    you have provided for it. No matter how long your buffer is, a user
    could always supply input that is longer. A well-written program
    reports invalid input with a comprehensible error message, not with a
    crash. (source)

    Nevertheless, their are some workaround that you can do to use scanf (check them here)

    Instead of scanf you can use fgets. fgets allows you to limit the data that will be placed in your buffer.

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

Sidebar

Related Questions

I have nested list of the following form: my_list = [['Some1', '2', '3.6', '4.5',
I have javascript object which consists of complex array of structures with nested structures
I have an array of nested JSON structures where they have varying depth and
I have the following problem: I have this multi-level array (nested array) which contains
I have a simple structure in mongodb, with nested array. How can I update
I have a nested array with valid numbers => data:- $validData = array(array(1 =>
I have a generated nested Array which I store some data. How can i
I have a nested associative array like this: $inputTypes= array( natural => array( text,
I have a nested php array. I have the first select populated already. I
I have a hierarchically nested associative array. It looks like this: A = {

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.