structures

This is a discussion on structures within the C Programming forums, part of the General Programming Boards category; Hello, I am a newbie to C programming, so forgive me if the questions I ask sound stupid. I am ...

  1. #1
    Registered User mlsbbe's Avatar
    Join Date
    Mar 2003
    Posts
    24

    structures

    Hello, I am a newbie to C programming, so forgive me if the questions I ask sound stupid.
    I am trying to create a program to calculate complex numbers using structures.
    I want to pass the value of the structure into the function and let the user change the value of the numbers. However, I keep getting this message:
    The left hand argument of '.' must be a structure not '<ptr>complex'.
    I have looked at some reference books, but i am still stumped on how to change the value of the structure in the function. Below is the code

    <code>
    #include <stdio.h>

    typedef struct complex{
    int real;
    int imaginary;
    };

    void take_values(complex *A);

    int main(void)
    {
    complex A;
    take_values(&A);
    printf("You have typed %d + j%d ", A.real,A.imaginary);

    return(0);
    }

    void take_values(complex *A)
    {

    printf("Please enter the real part of complex number, A: ");
    scanf("%d", A.real);
    printf("Please enter the imaginary part of complex number, B: ");
    scanf("%d",A.imaginary);

    }
    </code>

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Code:
    scanf("%d", A.real);
    Should be:
    Code:
    scanf("%d",A->real);
    Or
    Code:
    scanf("%d",(*A).real);

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You've also written the structure typedef incorrectly. It should be
    Code:
    typedef struct complex 
    {
      int real;
      int imaginary;
    } complex;
    And code tags are done with [] brackets.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    typedef struct complex
    {
    int real;
    int imaginary;
    } complex;
    Now I dont understand Hammer
    In my book it shows something like this
    Code:
    typedef struct {
       int real;
       int imaginary;
    } complex;
    Bahh lame book?

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There are various ways you can write it, but this isn't one AFAIK (from the OP)
    Code:
    typedef struct complex{
      int real;
      int imaginary;
    };
    Your example is fine.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    typedef struct NAME1
    {
       ...
    }NAME2;
    NAME1 is the name of the structure. If you want to create an object of it, you have to use the keyword struct in front:

    struct NAME1 MyStruct;

    NAME2 is the name of the typed definition that you make using typedef. Using typedefs creates a 'new datatype', so to make an object of it you just have to specify NAME2 instead of struct NAME1:

    NAME2 MyStruct;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    25
    Hello....i'm just a newbie here.....
    could annyone please...tell me if data struct in c is LIFO(last in first out) then the last value/variable that i have input should be the first one to be printed out? and how do I make a temporary memory location for the whole data struct / struct of arrays if i am gonna sort it? should i use/declare a temporary variable in the main() function || declare another data struct with the same as the one i have made....
    Last edited by v@grant; 03-10-2003 at 01:02 AM.
    TEACH ME....
    :-);-)

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>data struct in c is LIFO(last in first out)
    Neither. A struct is simply a number of variables grouped together. You access each element of the struct independantly.

    If you're talking about an array of structs, its the same principle. LIFO and FIFO are terms normally used for stacks and queues.


    >>how do I make a temporary memory location for the whole data struct / struct of arrays if i am gonna sort it?
    If you have an array you want to sort, lookup the qsort() function.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    25
    Ohh.. I see Thanx.... :-)
    TEACH ME....
    :-);-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-06-2006, 12:06 AM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 09:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 10:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 01:59 PM

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