Structures and instances.

This is a discussion on Structures and instances. within the C Programming forums, part of the General Programming Boards category; Hi everyone, I'm writing a program that creates 2 structures, with the second having an instance of the first structure. ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    42

    Structures and instances.

    Hi everyone,

    I'm writing a program that creates 2 structures, with the second having an instance of the first structure. I then create an instance of the second structure which the values are then in putted via the console, the program then creates a second instance of the second structure and copies all of the values from the user in putted structure.

    My program copies all of the values just fine except for the string and the the double.
    Can anyone spot any obvious flaws with my program?

    Thanks a lot
    Ryan

    Code:
    int main()
    {
    	struct Struct1
    	{
    		short int shortInt;
    		long int longInt;
    		char string[64];
    		double floating;
    	};
    
    	struct Struct2
    	{
    		short int shortInt;
    		long int longInt1;
    		long int longInt2;
    		struct Struct1 instance;
    	};
    
    	struct Struct2 My_Struct;
    
    	printf("Please enter a short integer: ");
    	scanf("%d", &My_Struct.shortInt );
    
    	printf("Please enter a long integer: ");
    	scanf("%d", &My_Struct.longInt1 );
    
    	printf("Please enter a second long integer: ");
    	scanf("%d", &My_Struct.longInt2 );
    	
    	printf("Please enter a short integer value, an instance of Struct1: ");
    	scanf("%d", &My_Struct.instance.shortInt );
    
    	printf("Please enter a long integer value, an instance of Struct1: ");
    	scanf("%d", &My_Struct.instance.longInt );
    
    	printf("Please enter a string, an instance of Struct1: ");
    	scanf("%s", &My_Struct.instance.string );
    
    	printf("Please enter a floating point number, an instance of Struct1: ");
    	scanf("%f", &My_Struct.instance.floating );
    
    	struct Struct2 My_Struct_2 = {
    									My_Struct.shortInt, My_Struct.longInt1, My_Struct.longInt2,
    									My_Struct.instance.shortInt, My_Struct.instance.longInt,
    									My_Struct.instance.string[64], My_Struct.instance.floating
    							     };
    
    	printf("The values that were entered and copied are: \n%d \n%d \n%d \n%d \n%d \n%s \n%f \n ",
    		My_Struct.shortInt, My_Struct.longInt1, My_Struct.longInt2,
    		My_Struct.instance.shortInt, My_Struct.instance.longInt,
    		My_Struct.instance.string[64], My_Struct.instance.floating);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Good heavens, you should pay attention to your compiler:
    Code:
    ryan.c: In function ‘main’:
    ryan.c:21: warning: implicit declaration of function ‘printf’
    ryan.c:21: warning: incompatible implicit declaration of built-in function ‘printf’
    ryan.c:22: warning: implicit declaration of function ‘scanf’
    ryan.c:22: warning: incompatible implicit declaration of built-in function ‘scanf’
    ryan.c:22: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘short int *’
    ryan.c:25: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘long int *’
    ryan.c:28: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘long int *’
    ryan.c:31: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘short int *’
    ryan.c:34: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘long int *’
    ryan.c:37: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[64]’
    ryan.c:40: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double *’
    ryan.c:44: warning: missing braces around initializer
    ryan.c:44: warning: (near initialization for ‘My_Struct_2.instance’)
    ryan.c:46: warning: missing initializer
    ryan.c:46: warning: (near initialization for ‘My_Struct_2.instance.floating’)
    ryan.c:51: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘long int’
    ryan.c:51: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘long int’
    ryan.c:51: warning: format ‘%d’ expects type ‘int’, but argument 6 has type ‘long int’
    ryan.c:51: warning: format ‘%s’ expects type ‘char *’, but argument 7 has type ‘int’
    ryan.c:42: warning: unused variable ‘My_Struct_2’
    ryan.c:52: warning: control reaches end of non-void function
    Your specific question comes from lines 44-46, where they talk about missing initializers. Once you start trying to initialize your char array with a single char, it all goes downhill from there.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You cannot assign array.
    When you have
    char a[10] = "hello";

    It's array initialization not assignment.
    char a[10];
    a = "hello"; // compile error

    You could just simply assign struct.

    struct my_struct a,b;
    // init b
    a = b;

    But take note that it's the same as memcpy(a,b,sizeof(struct my_struct) );
    If there're pointers in struct, only pointer values are copied.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    Apologies , but i still dont understand what is wrong with the string, the value printed is <null> on the console.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,157
    What is your current code after responding to the replies in this thread?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    Bayint Naung, Showed me a more efficient way of copying the instances, that's all I understood though. My book has a sample program in it and declares the array of string the exact same way i have.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    	struct Struct1
    	{
    		short int shortInt;
    		long int longInt;
    		char string;
    		double floating;
    	};
    
    	struct Struct2
    	{
    		short int shortInt;
    		long int longInt1;
    		long int longInt2;
    		struct Struct1 instance;
    	};
    
    	struct Struct2 My_Struct, My_Struct_2;
    
    	printf("Please enter a short integer: ");
    	scanf("%d", &My_Struct.shortInt );
    
    	printf("Please enter a long integer: ");
    	scanf("%d", &My_Struct.longInt1 );
    
    	printf("Please enter a second long integer: ");
    	scanf("%d", &My_Struct.longInt2 );
    	
    	printf("Please enter a short integer value, an instance of Struct1: ");
    	scanf("%d", &My_Struct.instance.shortInt );
    
    	printf("Please enter a long integer value, an instance of Struct1: ");
    	scanf("%d", &My_Struct.instance.longInt );
    
    	printf("Please enter a string, an instance of Struct1: ");
    	scanf("%s", &My_Struct.instance.string );
    
    	printf("Please enter a floating point number, an instance of Struct1: ");
    	scanf("%f", &My_Struct.instance.floating );
    
    	My_Struct_2 = My_Struct;
    	
    	printf("The values that were entered and copied are: \n%d \n%d \n%d \n%d \n%d \n%s \n%f \n ",
    		My_Struct_2.shortInt, My_Struct_2.longInt1, My_Struct_2.longInt2,
    		My_Struct_2.instance.shortInt, My_Struct_2.instance.longInt,
    		My_Struct_2.instance.string, My_Struct_2.instance.floating);
    }

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    I Seem to have fixed the problem without realising in terms of the string, although my double value still prints as some ridiculous negative number regardless of what i enter, is this a formatting problem?

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Yes, Read the documentation of the functions you used if you are not sure.
    scanf format specifier for double is %lf , not %f.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    In the scanf...
    for int, use format "%d"
    for long use format "%ld"
    for double use format "%lf"

    Same for the printf.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    	struct Struct1
    	{
    		short int shortInt;
    		long int longInt;
    		char string;
    		double floating;
    char string; should be char string[somesize], as in char string[64];

    char string is only 1 character ... if you want to put "hello" or something meaningful in there you need to initialize an array to hold the characters entered.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ confusion
    By Eirik in forum Windows Programming
    Replies: 14
    Last Post: 04-29-2009, 02:54 PM
  2. Question about variables in structures
    By Donarstan in forum C Programming
    Replies: 1
    Last Post: 03-23-2008, 05:43 AM
  3. Replies: 15
    Last Post: 12-07-2006, 09:27 PM
  4. Several instances of the same resource/file
    By knutso in forum Windows Programming
    Replies: 0
    Last Post: 09-09-2004, 05:30 AM
  5. creating structures help
    By bobnet in forum C++ Programming
    Replies: 4
    Last Post: 08-25-2003, 09:46 PM

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