Array and Structure Question

This is a discussion on Array and Structure Question within the C++ Programming forums, part of the General Programming Boards category; I under stand why you have to use a Multidimensional Array to make a string: #include <iostream.h> #include <string.h> main() ...

  1. #1
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195

    Array and Structure Question

    I under stand why you have to use a Multidimensional Array to make a string:

    #include <iostream.h>
    #include <string.h>

    main()
    {
    char name[2][8];
    int endprogram;

    strcpy(name[0],"Hello");
    strcpy(name[1],"David");
    cout << name[0] << endl;
    cout << name[1];
    cin >> endprogram;
    return(0);
    }

    But I do not understand why, when using a structure, you dont have to use a Multidimensional Array for the field:

    #include <iostream.h>
    #include <string.h>

    struct planet {
    char x[11];
    };

    main()
    {
    int endprogram;
    struct planet name;

    strcpy(name.x,"Hello");
    cout << name.x << endl;
    cin >> endprogram;
    return(0);
    }

    Can some one please explain this to me? I look like this guy at the moment

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    First off, please use code tags to post code, very hard to read!

    Second off, you DON'T need a multidimensional array for a string. In your first example you actually have two strings, no different than if you had an array of ints. For example, this works just fine:
    Code:
    #include <iostream.h>
    #include <string.h>
    
    main()
    {
    char name[8];
    int endprogram;
    
    strcpy(name,"Hello");
    cout << name << endl;
    cin >> endprogram;
    return(0);
    }

  3. #3
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    O ok i thought that you did not have to use a 2D array, but when i tested it i accedently called on an element number like a retard instead of just listing the variable, now i understand thanks for pointing it out.

    Now i look like this guy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 11:10 PM
  3. Multidimensional Array in a Structure
    By Night_Blade in forum C Programming
    Replies: 3
    Last Post: 04-04-2005, 09:14 PM
  4. array in structure help
    By bobnet in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2003, 08:51 AM
  5. Filling an array in a structure
    By thephreak6 in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21