Input for Struct?

This is a discussion on Input for Struct? within the C Programming forums, part of the General Programming Boards category; When I compile, it gives me this error: Code: a8-2.c: In function `main': a8-2.c:26: warning: implicit declaration of function `strdup' ...

  1. #16
    Registered User
    Join Date
    Apr 2004
    Posts
    18
    When I compile, it gives me this error:

    Code:
    a8-2.c: In function `main':
    a8-2.c:26: warning: implicit declaration of function `strdup'
    a8-2.c:26: warning: assignment makes pointer from integer without a cast
    Any clues? I've never used "strdup" before, do I need another header at the top like "#include ___" to make it work?

  2. #17
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477

    Post

    Oh, well

    Yes you will need to #include <string.h>

    strdup() simply works something like this:

    Code:
    char *strdup(char *s) {
    	int len;
    	char *dup = NULL;
    
    	len = strlen(s) + 1;
    	if ((dup = (char *) malloc(len)) != NULL)
    		memcpy(dup, s, len);
    	return (dup);
    }
    Either you can use that instead of #include <string.h>, or you can include it and use theirs in the library.

    If it still gives you an error about strdup() I'll see if I can find another route to writing member[].name, like malloc(sizeof(buffer)), etc...


    We'll see what happens,
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #18
    Registered User
    Join Date
    Apr 2004
    Posts
    18
    I actually fixed the input so it reads it like this:

    Code:
     for (i = 0; i < k; i++) {
    
                    fflush(stdin);
                    printf("Please enter name: ");
                    fgets(buffer, sizeof(buffer), stdin);
                    members[i].name = malloc (sizeof buffer);
                    strcpy(members[i].name,buffer);
    
                    printf("Please enter ID:   ");
                    scanf("%d", &members[i].id);
                    members[i].index = i;
            }
    Which seems to work as far as inputting goes. The bubblesort works now for the numbers, but it does something weird to the names. I will show you my input and output:

    INPUT:

    Code:
    Please enter name: a
    Please enter ID:   9
    Please enter name: b
    Please enter ID:   3
    Please enter name: c
    Please enter ID:   5
    Please enter name: d
    Please enter ID:   7
    Please enter name: e
    Please enter ID:   1
    Please enter name: f
    Please enter ID:   100
    Please enter name: g
    Please enter ID:   101
    Please enter name: h
    Please enter ID:   99
    Please enter name: i
    Please enter ID:   1000
    Please enter name: j
    Please enter ID:   999
    So the Output SHOULD Be:

    Code:
    1, f
    
    3, c
    
    5, d
    
    7, e
    
    9, b
    
    99, h
    
    100, f
    
    101, g
    
    999, j
    
    1000, i
    But instead it is:

    Code:
    1, b
    
    3, a
    
    5, b
    
    7, c
    
    9, d
    
    99, g
    
    100, f
    
    101, g
    
    999, j
    
    1000, i
    It seems like the last few worked, but the first ones didn't. Very weird...any ideas?

  4. #19
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477

    Post

    Hi,

    I'm sorry about that I messed up on the bubble sort, here is the new one:

    Code:
    void bubbleSort(int numbers[], int array_size) {
    	int i, j, temp;
    
    	for (i = (array_size - 1); i >= 0; i--) {
    		for (j = 1; j <= i; j++) {
    			if (numbers[j-1] > numbers[j]) {
    				temp = numbers[j-1];
    				numbers[j-1] = numbers[j];
    				numbers[j] = temp;
    
    				// Our addition (resort index)
    				temp = members[j-1].index;
    				members[j-1].index = members[j].index;
    				members[j].index = temp;
    			}
    		}
    	}
    }
    Also, to get the output you are looking for, you should call on the ID first, not the name because doing:

    Please enter name: a
    Please enter ID: 9
    Please enter name: b


    Only results to:

    9, b

    So now, it should work something like:

    Code:
    for (i = 0; i < k; i++) {
    	fflush(stdin);
    	printf("Please enter name: ");
    	fgets(buffer, sizeof(buffer), stdin);
    	members[i].name = (char *)malloc(sizeof(buffer));
    	strcpy(members[i].name,buffer);
    
    	printf("Please enter ID:   ");
    	scanf("%d", &members[i].id);
    
    	members[i].index = i;	// store index
    }
    Then:
    Code:
    for (j = 0; j < k; j++) {
    	printf("%d, %s\n", numbers[j], members[members[j].index].name);
    	free(members[j].name);	// free memory!
    }
    I added a new line to the 'j' loop that free's the allocated memory once its all done reading.

    EDIT: You can do name first, I fixed the bubblesort duh it will work now.

    Hope this helps,
    - Stack Overflow
    Last edited by Stack Overflow; 05-03-2004 at 11:03 AM.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  5. #20
    Registered User
    Join Date
    Apr 2004
    Posts
    18
    This doesn't make any sense why it's doing this, but my input/output is as follows:

    Code:
    Please enter ID:   10
    Please enter name: a
    Please enter ID:   9
    Please enter name: b
    Please enter ID:   8
    Please enter name: c
    Please enter ID:   7
    Please enter name: d
    Please enter ID:   6
    Please enter name: e
    Please enter ID:   5
    Please enter name: f
    Please enter ID:   4
    Please enter name: g
    Please enter ID:   100
    Please enter name: h
    Please enter ID:   1000
    Please enter name: i
    Please enter ID:   999
    Please enter name: j
    4, b
    
    5, a
    
    6, b
    
    7, c
    
    8, d
    
    9, e
    
    10, f
    
    100, h
    
    999, j
    
    1000, i
    Why on earth would it work for the last 3 and not work for the others? Doesn't make sense to me...I've read the bubbelsort code and can't make sense of it.

    Confused, very very confused,
    Stewade

  6. #21
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477

    Post

    Wait, wait,

    I made an edit to my previous post, and I'll post here. I'm sorry, since I fixed the bubbleSort() you can do name first. Also I had a glitch, do not free the memory until the reading is over:

    Code:
    int r;
    
    // (members[j].index) is the index we need
    for (j = 0; j < k; j++) {
    	printf("%d, %s\n", numbers[j], members[members[j].index].name);
    }
    
    // FIX: keep memory free seperate (causes trouble if not)
    for (r = 0; r < k; r++) {
    	free(members[r].name);
    }
    Then this should work:

    Please enter name: Johnny
    Please enter ID: 321
    Please enter name: Stewart
    Please enter ID: 123
    Then results to:

    123, Stewart
    321, Johnny
    Sorry for the confusion, that was my bad. It should work with name first, no worries.

    - Stack Overflow
    Last edited by Stack Overflow; 05-03-2004 at 11:22 AM.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  7. #22
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477

    Post

    You should be able to restore your original code to something like:

    Code:
    for (i = 0; i < k; i++) {
    	fflush(stdin);
    
    	printf("Please enter name: ");
    	fgets(buffer, sizeof(buffer), stdin);
    	members[i].name = (char *)malloc(sizeof(buffer));
    	strcpy(members[i].name, buffer);
    
    	printf("Please enter ID:   ");
    	scanf("%d", &members[i].id);
    
    	members[i].index = i;	// store index
    }

    Hope this helps,
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  8. #23
    Registered User
    Join Date
    Apr 2004
    Posts
    18
    Seems to be having the same problem. I think the problem might be in bubblesort, but I can't seem to figure it out, the code looks correct to me

    Code:
    Please enter name: stewart
    Please enter ID:   100
    Please enter name: johnny
    Please enter ID:   55
    Please enter name: liz
    Please enter ID:   23
    Please enter name: abby
    Please enter ID:   10002
    Please enter name: nancy
    Please enter ID:   345
    Please enter name: drew
    Please enter ID:   456
    Please enter name: mark
    Please enter ID:   333
    Please enter name: dennis
    Please enter ID:   1234
    Please enter name: joe
    Please enter ID:   999
    Please enter name: gill
    Please enter ID:   123445
    23, johnny
    
    55, stewart
    
    100, johnny
    
    333, nancy
    
    345, abby
    
    456, nancy
    
    999, dennis
    
    1234, mark
    
    10002, dennis
    
    123445, gill

  9. #24
    Registered User
    Join Date
    Apr 2004
    Posts
    18
    Here's my bubblesort code I'm using for reference to my previous post:

    Code:
    void bubbleSort(int numbers[], int array_size) {
            int i, j, temp;
    
            for (i = (array_size - 1); i >= 0; i--) {
                    for (j = 1; j <= i; j++) {
                            if (numbers[j-1] > numbers[j]) {
                                    temp = numbers[j-1];
                                    numbers[j-1] = numbers[j];
                                    numbers[j] = temp;
    
                                    members[j-1].index = j;
                                    members[j].index = j-1;
                            }
                    }
            }
    }

  10. #25
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477

    Post

    Try this one:

    Code:
    void bubbleSort(int numbers[], int array_size) {
    	int i, j, temp;
    
    	for (i = (array_size - 1); i >= 0; i--) {
    		for (j = 1; j <= i; j++) {
    			if (numbers[j-1] > numbers[j]) {
    				temp = numbers[j-1];
    				numbers[j-1] = numbers[j];
    				numbers[j] = temp;
    
    				// Our addition (resort index)
    				temp = members[j-1].index;
    				members[j-1].index = members[j].index;
    				members[j].index = temp;
    			}
    		}
    	}
    }
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  11. #26
    Registered User
    Join Date
    Apr 2004
    Posts
    18
    WAHOOOOOOOOOOOOO! Man alive, thank you so much. You are a lifesaver. I really really appreciate it!

  12. #27
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477

    Talking

    ,

    Glad every thing is working now! (or almost) hehe. Good luck with the program, project, assignment, or whatever it is I'm sure you'll do well, and I hope that I have been of some help.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. continues input
    By Matty_Alan in forum C Programming
    Replies: 2
    Last Post: 06-22-2007, 10:04 PM
  2. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  3. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  4. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM
  5. Help with Input Checking
    By Derek in forum C Programming
    Replies: 7
    Last Post: 06-17-2003, 03:07 AM

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