Setting a string to null

This is a discussion on Setting a string to null within the C Programming forums, part of the General Programming Boards category; How would I set a string to NULL from another function? I tried to use Code: strcpy(string, NULL); but it ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    20

    Setting a string to null

    How would I set a string to NULL from another function? I tried to use

    Code:
    strcpy(string, NULL);
    but it does not seem to work and I've also tried

    Code:
    *string=NULL;
    and that did not help either...
    I've tried looking at previous forums and did a google search but can't seem to find it. Seems like it should be a simple answer but I'm just missing it.. Can anyone help?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    By setting a "string to NULL", what are you trying to do? Do you have a pointer and want to make it a null pointer? Or do you have a string and you want to make it an empty string?

    >strcpy(string, NULL);
    This isn't a good idea, strcpy expects both arguments to be non-null pointers to C-style strings.

    >*string=NULL;
    This isn't a good idea either, NULL should only be used in pointer context. The assignment reeks of NULL being used in character context, in which case you want:
    Code:
    *string = '\0';
    My best code is written with the delete key.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well it depends on what "string" is. If its just a pointer then:
    Code:
    char *string = NULL
    will work. If string is an array of characters then you can do:
    Code:
    char string[10];
    string[0] = '\0';
    or
    Code:
    memset(string, '\0', sizeof string);
    Inside another function you could have:
    Code:
    char *foo(char *bar)
    {
      return NULL;
    }
    if its a pointer or
    Code:
    void foo (char *bar, int size)
    {
      bar[0]='\0';
      memset(bar, '\0', size);
    }

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    20
    Thank you both. I know I needed to be more specific... Sorry about that. The '\0' worked. Thanks a lot you two.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 12:29 PM
  2. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM

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