Hey guys, I'm just trying to get to grasp with structs and I have written a small test program to print out a persons name and age. Currently I am getting nothing when the program runs. Help :(

#include <stdio.h>

struct person
{
  char *name;
  int age;
};

struct person people[5];

int main( void )
{

  people[0].name = "Bob";
  people[0].age = 45;
  people[1].name = "Steve";
  people[1].age = 32;

  int i;
  for(i = 0; i < 5; i++)
  {
    char *value = people[i].name;
    int value2 = people[i].age;

    printf("%s %d\n", value, value2);
  }    
  return(0);

Recommended Answers

I just ran your program and got results..What are you compiling/running this on?

Jump to Post

All 3 Replies

I just ran your program and got results..What are you compiling/running this on?

Member Avatar
char *value = people[i].name;

is potentially dangerous because you did not initialize / declare a variable for the name member from indices 2 to 4. Due to this, you might get garbage results, or the program could crash.

char *value = people[i].name;

is potentially dangerous because you did not initialize / declare a variable for the name member from indices 2 to 4. Due to this, you might get garbage results, or the program could crash.

Not really, the structure was created in the global space so it will be bitwise zeroed..So you'll have zeros and not garbage.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.