Thread: Accessing a struct.

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    Accessing a struct.

    Using the red OR the green will have the same result (will access the struct in the same way)?

    Code:
    #include <stdio.h>
    
    struct Info{
    	float amount;
    	char fname[30];
    	char lname[30];
    }rec;
    
    int main(void){
            struct Info rec;
    	printf("Enter the whole name:\n");
    	scanf("%s %s", rec.fname, rec.lname);
    	
    	printf("Please now enter the amount:\n");
    	scanf("%f", &rec.amount);
    	
    	printf("Mr %s %s gave an amount of %.2f $.\n", rec.fname, rec.lname, rec.amount);
    
    return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by brack View Post
    Using the red OR the green will have the same result (will access the struct in the same way)?
    It should... just don't try red AND green, your compiler will rebel.
    Last edited by CommonTater; 09-03-2010 at 04:48 PM. Reason: oops....

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, one difference is that the green version makes the struct object global, which may be undesirable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Quote Originally Posted by CommonTater View Post
    It should... just don't try red AND green, your compiler will rebel.
    Anyway this is why i said OR...thanks for your answer...
    Last edited by brack; 09-03-2010 at 04:52 PM.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    So althoung It depends from the program, most times i should use the red one...?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by CommonTater View Post
    It should... just don't try red AND green, your compiler will rebel.
    Why would it rebel? It might be kind enough to give you a warning that you are reusing a file global variable variable name for a local automatic variable. It also may be kind enough to give you a warning that the global struct rec with external linkage and static duration is unused. But it is perfectly fine from the compiler's point of view for a local automatic variable to have the same name rec as a file global variable. Hopefully, your code reviewers would rebel because it is hard to maintain code that uses the same name for a variable as a file global and a local variable.
    Last edited by pheininger; 09-03-2010 at 09:22 PM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It should also be stated that you should not use scanf to read strings. You should use fgets.
    SourceForge.net: Scanf woes - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cant understand Linked list example:
    By satty in forum C Programming
    Replies: 15
    Last Post: 08-13-2010, 10:12 AM
  2. C problem - Create a Personal Library
    By Harliqueen in forum C Programming
    Replies: 33
    Last Post: 04-20-2010, 11:27 PM
  3. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  4. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM