Thread: character strings

  1. #1
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198

    character strings

    i'm just wondering when it's ok to use a declaration such as char *string; instead of something like char string[BUFSIZ]; because when I try to store user input into the first one it has a tendency to give me trouble. but the second one works fine. as far as i know the first one doesn't assign any memory to the pointer because it doesn't know how much, but i have seen plenty of code that doesn't use anything like malloc to assign memory before storing strings, so why am i having problems? if it matters, i'm on windows 2k pro using dev-cpp 4.9.8.9

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > but i have seen plenty of code that doesn't use anything like malloc to assign memory before storing strings
    All such code is wrong - unfortunately, much of it originates on DOS where uninitialised pointers could be used with abandon (for a short while at least).

    Every pointer variable needs a malloc of some sort, or be made to point at some other part of memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    i'm just wondering when it's ok to use a declaration such as char *string; instead of something like char string[BUFSIZ];
    Only when you initalize the pointer immediately such as:
    Code:
    char *str = "Hello Bob";
    Of course then you can not modify the string in any way, shape, or form. A better way would be
    Code:
     const char *str = "Hello Bob";
    Of course with both you are just initalizing the pointer to the location of "Hello Bob".

    If you want to make any changes to the contents then you have to malloc.

    Remember all variables should be initalized upon declaration including (and especially) pointers.

  4. #4
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    ok that all makes sense, but then I wonder why you guys are so picky when people post stuff like void main() and then the same people go ahead and post code like
    Code:
    int main()
    {
      char *stuff;
    
      fgets(stuff, 256, stdin);
      printf("you entered %s.\n");
    
      return 0;
    }
    i just made that up but i mean that's the kinda stuff you see from the same people who bash others for doing void main() (which by the way i know is wrong)
    and i'll remember to initialize all my auto variables upon declaration

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Oh we pick those up as well
    Even more so if they use gets() instead of fgets()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    We are equal opportunity bashers

  7. #7
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    awesome, so i'll remember to keep using void main() and gets() whenever possible but DARN you for using scanf() for user input!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  2. Character strings
    By campermama in forum C++ Programming
    Replies: 1
    Last Post: 06-17-2004, 04:26 PM
  3. character strings and reading in a file...
    By Unregistered in forum C Programming
    Replies: 14
    Last Post: 07-30-2002, 09:51 AM
  4. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM
  5. Array of character strings
    By nima_ranjbar in forum C++ Programming
    Replies: 1
    Last Post: 02-08-2002, 09:37 AM