Thread: Any Help Appreciated

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    14

    Any Help Appreciated

    I am trying to use the take input from a user and store the informatin that is input on the heap. I am close.. (i think) but keep getting errors. The problem occurs when the user chooses option #3 and begins to input data.

    My files are attached.

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    OK i though my files were attached.. here they are

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>if(len = strlen(p->name) > NAME_SZ)
    How many equals signs needed to make a comparison?

    >>char *filename;
    >>scanf("%s", filename);
    filename is a pointer, not an array. I'd suggest:
    >>char filename[BUFSIZ];

    In main():
    >>if(size >= MAX)
    What value does size have when you invoke this line of code? Think about it....

    There are more things too, see how you get on....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172
    If you want dynamic allocation, try:

    Code:
    char *filename;
    filename = malloc(100);
    scanf("%s", filename);
    I've heard bad things about 'scanf()', the second line of code above will at least solve running into the end of an array.

    {edit}
    Last edited by loopy; 05-04-2004 at 01:16 AM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by loopy
    If you want dynamic allocation, try:
    Code:
    char *filename;
    *filename = malloc(100);
    scanf("%s", filename);
    I've heard bad things about 'scanf()', the second line of code above will at least solve running into the end of an array.
    1) You're dereferencing filename, and are trying to assign the return of malloc there. That's wrong. Lose the first *. Because you're making an assignment to a pointer, not to what it dereferences to.

    2) This eliminates running off the end of an array, true. But you can still blow by the end of your malloced memory. All I have to do is enter 101 characters without any white space and I've gone off the end.

    The FAQ covers safer ways of getting input if anyone's interested.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172
    Quote Originally Posted by quzah
    1) You're dereferencing filename, and are trying to assign the return of malloc there. That's wrong. Lose the first *. Because you're making an assignment to a pointer, not to what it dereferences to.

    2) This eliminates running off the end of an array, true. But you can still blow by the end of your malloced memory. All I have to do is enter 101 characters without any white space and I've gone off the end.

    The FAQ covers safer ways of getting input if anyone's interested.

    Quzah.
    Thanks, I've edited it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  2. Program Control, HELP appreciated.
    By Astra in forum Windows Programming
    Replies: 7
    Last Post: 01-01-2007, 06:59 AM
  3. Simple question. Help will be appreciated.
    By wickedclownz in forum C++ Programming
    Replies: 2
    Last Post: 06-19-2003, 02:18 AM
  4. C++ Programs -- Any input appreciated
    By Kyoto Oshiro in forum Game Programming
    Replies: 0
    Last Post: 02-27-2002, 11:22 PM
  5. I need help..anything will be appreciated
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2001, 10:55 AM