Assigning variables to elements of structs

This is a discussion on Assigning variables to elements of structs within the C Programming forums, part of the General Programming Boards category; Hey, I'm writing a program that takes user input (names, addresses, phone numbers) puts it into a struct and keeps ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    4

    Assigning variables to elements of structs

    Hey,
    I'm writing a program that takes user input (names, addresses, phone numbers) puts it into a struct and keeps those records. I'm just getting stuck with how to properly assign the input to the elements. Here's my code

    Code:
    struct rec {
    	char *name;
    	char *address;
    	short addressLength, nameLength;
    	int phoneNumber;
    };
    
    ...
    
    int newRec()
    {
    	char *nm;
    	char *addy;
    	int nLength, aLength;
    	int phone;
    	
    	printf("\nEnter the Name: ");
    	scanf("%s", &nm);
    	printf("\nEnter the Address: ");
    	scanf("%s", &addy);
    	printf("\nEnter the telephone number: ");
    	scanf("%d", &phone);
    	
    	struct rec record;
    
    	record.name = (char) malloc(strlen(nm)+1);
    	
    	if (record.name == NULL) 
    	{
        		printf("Error");
        		return 1;
      	}
    
    	nLength = strlen(nm)+1;
    	record.address = (char*) malloc(strlen(addy)+1);
    
    	if (record.address == NULL) 
    	{
        		printf("Error");
        		return 1;
      	}
    
    	aLength = strlen(addy)+1;
    	strcpy(record.name, nm);
    	strcpy(record.address, addy);
    
    	record.addressLength = aLength;
    	record.nameLength = nLength;
    	record.phoneNumber =  phone;
    I've also tried with scanf and I constantly get a segmentation fault, and corrupted stack after entering the address or the telephone number. I'm not well versed with structs, any help would be greatly appreciated!

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
    	char *nm;
    	char *addy;
    	int nLength, aLength;
    	int phone;
    	
    	printf("\nEnter the Name: ");
    	scanf("%s", &nm);
    	printf("\nEnter the Address: ");
    	scanf("%s", &addy);
    Since "nm" and "addy" are already pointers, you don't have to pass their addresses. So for these two "scanfs", remove the "&".

    For "nm" and "addy", you must allocate memory for them before using them. You seem to be doing this correctly when you dynamically allocate memory for the corresponding variables in the struct (namely, "name" and "address"). However, if the above quoted code was correct, then you could do the exact same thing for the variables in struct (i.e. simply assign or scanf directly into the "char*"s). Unfortunately, its not.

    So for "nm" and "addy", you must allocate memory. However, since you (probably) don't know ahead of time how long these values are going to be, you're just gonna have to choose a "max". Of course you could ask the user "How long is your name?" But this would be an extremely poor UI. So, just try and choose a "reasonable" maximum size, and declare these two variables as something like
    Code:
     char nm[64];
    char address[64];
    Its common to see these maximum sizes as some power of 2, i.e. 16, 32, 64, etc., but its certainly not enforced.

    As for casting "malloc": Cprogramming.com FAQ > Casting malloc.
    Last edited by nadroj; 03-08-2010 at 08:24 PM.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    26
    In your code,

    Code:
            char *nm;
    	char *addy;
    	int nLength, aLength;
    	int phone;
    	
    	printf("\nEnter the Name: ");
    	scanf("%s", &nm);
    	printf("\nEnter the Address: ");
    	scanf("%s", &addy);
    You have declared nm and addy as a char pointer.Then in scanf you want to get a input for these pointers without allocating memory.

    So first allocate memory for these pointers and use it in scanf.

    And in scanf you no need to specify '&' for pointers.

    So change your code like,

    Code:
    nm=(char *)malloc(100);
    addy=(char *)mallloc(100);
    scanf("%s",nm);

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    4
    Awesome guys, thanks so much!

    I probably should've realised that :P

    Again, thank you.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    Before you leap for joy, I have a few words of caution:
    • You should free() what you malloc().
    • As nadroj has noted, you should not cast the return value of malloc (unless you need your code to be compilable as C++).
    • Using the %s format specifier makes your code vulnerable to buffer overflow unless you also specify a field width.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  2. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 10:07 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM

Tags for this Thread


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