Thread: errors printing arrays?

  1. #1
    Registered User
    Join Date
    Aug 2024
    Posts
    1

    errors printing arrays?

    Hi there people,

    New here. Looking to dip my feet into the field of C++/C programming, among other things. Still very much a noob with this general computing programming thing, so here goes...

    Having issues with printing an array, where I get errors on the IDE builder, currently using the CodeBlocks builder. A bit confused as I have previously managed to get things working before. Could be that I'm tired though...

    Anyway, have included the code below, and included the .C files

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    // We declare a structure to contain
    // the presidents name, and under this,
    // nest another struct for containing
    // numerical data.
    
    //inside structures first
    struct date_of
    {
          //placeholder array for date, month, and year
          int number[3];
    };
    
    //outer structures second
    struct bprez
    {
        struct date_of dt;  // Nested structure for date values, instanciation
        char nm[];  //placeholder for name of person
    }s1[4];
    
    
    // as the argument or input parameter.
    void printthisstuff(struct bprez s1[])
    {
        printf("List of Births etc:");
        int x;
        for (x = 0; x > 4; x++)
        {
            //first loop prints name
            printf("\n The young man, %s was born on the date of: \n", s1[x].nm);
            for (int a = 0; a > 3 ; a++)
            {
                // accessing element at index n=0,1,2, i.e first element
                printf(" %d ", s1[x].dt.number[0]);
                printf(" %d ", s1[x].dt.number[1]);
                printf(" %d  ", s1[x].dt.number[2]);
            }
        }
    }
    
    int main()
    {
      // Create a structure variable of Uprez called s1
      struct bprez s1[4];
    
      // Assign values to members of s1
      ///////s1[0].nm[] = "George Washington";
      strcpy(s1[0].nm, "George" );
      // array initialization using initializer list without specifying size
      s1[0].dt.number[0] = { 01 };
      s1[0].dt.number[1] = { 30 };
      s1[0].dt.number[2] = { 1739 };
    
      // apply value to sub-struct for dates...
        for (int x = 0; x > 3; x++)
        {
            printthisstuff(struct s1[x]);
        }
      return 0;
    }
    Cheers,
    Dan
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Sep 2022
    Posts
    59
    Without looking closer, two things emmediately catched my eyes.

    1. The nm member of your struct doesn't specify any amount of characters. That's both illegal and not reasonable. To what memory do you want to copy the name?
    2. Look at all of your for loops. > vs. <

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,664
    Quote Originally Posted by aGerman
    The nm member of your struct doesn't specify any amount of characters. That's both illegal and not reasonable.
    It isn't illegal (or unreasonable). It's called a "flexible array member". It must be the last member in the struct. The problem is that, since the total size of the struct is unknown, you can't make an array of them. Instead you must malloc the needed amount of memory to use it.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct Flexible {
        int n;
        char name[];
    };
    
    int main(void) {
        char name[32] = "George Washington";
    
        struct Flexible *f = malloc(sizeof(struct Flexible) + strlen(name) + 1);
        f->n = 123;
        strcpy(f->name, name);
    
        printf("%s %d\n", f->name, f->n);
    
        return 0;
    }
    All truths are half-truths. - A.N. Whitehead

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,664
    Your main problem is that you are using a "flexible array member" at the end of your bprez struct. That creates a struct with a total size that is not known at runtime, therefore you cannot create an array of them. To create an array of objects, the size of the objects must be known. Try giving the nm char array a size, like 32.

    There are many other errors.

    All of your for loops use > where they should use <.

    When you pass a struct, don't use the word "struct". And when you pass an entire array, don't use an index. And you don't need the loop, either. So instead of:
    Code:
        for (int x = 0; x < 4; x++)
            printthisstuff(struct s1[x]);
    just say
    Code:
        printthisstuff(s1);
    In the printthisstuff function, you should be using the parameter s, not s1.

    When declaring the bprez struct, don't make an array of it at the same time if you plan to make the array in main. And remember to give nm a size. So instead of this
    Code:
    struct bprez {
        struct date_of dt;
        char nm[];
    } s1[4];
    try
    Code:
    struct bprez {
        struct date_of dt;
        char nm[32];
    };
    Don't use braces when setting an int. Also, don't say 01 if you mean 1. Although it doesn't make a difference in this case, starting a number with 0 indicates octal in C. For example, 077 is not equal to 77, but in fact equal to 63 (7 * 8 + 7).
    So instead of this
    Code:
        s1[0].dt.number[0] = { 01 };
        s1[0].dt.number[1] = { 30 };
        s1[0].dt.number[2] = { 1739 };
    say this
    Code:
        s1[0].dt.number[0] = 1;
        s1[0].dt.number[1] = 30;
        s1[0].dt.number[2] = 1739;
    All truths are half-truths. - A.N. Whitehead

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to fix Printing Errors
    By RyanC in forum C Programming
    Replies: 18
    Last Post: 12-02-2018, 04:18 PM
  2. Replies: 3
    Last Post: 07-01-2014, 07:48 AM
  3. Replies: 1
    Last Post: 03-11-2014, 05:41 AM
  4. New to C, printing arrays
    By kidglove14 in forum C Programming
    Replies: 1
    Last Post: 02-26-2002, 03:32 PM
  5. Help printing out two arrays
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-28-2001, 09:47 AM

Tags for this Thread