Thread: Load value to struct.

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    11

    Load value to struct.

    Say I have the following structs:

    Code:
    struct stA
    {
         int Num;
         stB Other[100];
    }One;
    
    struct stB
    {
         char list[100];
         int Num2;
    }Two;
    then how would I go about putting a string value into the second struct type stB in position 0 in the location list?

    This is all I can figure, but when I print it all I get is a whole screem of jiberish:

    Code:
    char C[100] = "asdf";
    
    One.Two[0].list = C[100];
    It just doesnt work.. anyone got an idea? do I have to declare it some how else?? I think there is a way to do it using curly brackets but I couldnt get that to work either.. HELP!

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    But struct stB will have to be above/before struct stA.

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    struct stB
    {
         char list[100];
         int Num2;
    }Two;
    
    
    struct stA
    {
         int Num;
         stB Other[100];
    }One;
    
    
    
    
    int main()
    {
    
    	
    
    
    	cin >> One.Other[0].list;
    
    	cout << One.Other[0].list << endl;
    
    
    
    
    
    	return 0;
    
    }

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    Ok, I would image this would be the correct way to put the string into the type stB struct, but I get the following error:

    Code:
    One.Two[0] = {"asdf",1};
    I get the error

    Code:
    : error C2059: syntax error : '{'
    : error C2143: syntax error : missing ';' before '{'
    : error C2143: syntax error : missing ';' before '}'
    EDIT: But I cant move them do to my problem.. is there any way of getting that string in that location?

  4. #4
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    You are getting confused on what it an array and what is not.
    "Two" is a struct of type stB, there is only one "Two", but that "Two" has an array in it called list.
    Two.list[i] is valid;
    There is also only one "One" of struct type atA, but that one "One" has an array of type stB.
    So "One" has an array size 100 of type stB; which in turn has an array of size 100 in it.
    So you now have 100 things, and each can hold 100 things.

    If it was One[2]; you would now have an array of stA, in which each one would have an array of size 100 of stB which in each one of those would have a char array of size 100.


    And that is very confusing and why we use more descriptive names for our variables and never use something like “one” or “two”!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM