Your line char *str = '\0'; actually DOES set str to (the equivalent of) NULL. This is because '\0' in C is an integer with value 0, which is a valid null pointer constant. It's extremely obfuscated though :-)

Making str (a pointer to) an empty string is done with str = ""; (or with str = "\0";, which will make str point to an array of two zero bytes).

Note: do not confuse your declaration with the statement in line 3 here

char *str;
/* ... allocate storage for str here ... */
*str = '\0'; /* Same as *str = 0; */

which does something entirely different: it sets the first character of the string that str points to to a zero byte, effectively making str point to the empty string.

Terminology nitpick: strings can't be set to NULL; a C string is an array of characters that has a NUL character somewhere. Without a NUL character, it's just an array of characters and must not be passed to functions expecting (pointers to) strings. Pointers, however, are the only objects in C that can be NULL. And don't confuse the NULL macro with the NUL character :-)

Answer from Jens on Stack Overflow
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 58206-setting-string-null.html
Setting a string to null
October 24, 2004 - 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.
Discussions

c - Make a string null in a single line - Stack Overflow
C strings are null-terminated. As long as you only use the functions assuming null-terminated strings, you could just zero the first character. ... Sign up to request clarification or add additional context in comments. More on stackoverflow.com
๐ŸŒ stackoverflow.com
November 16, 2011
c - setting a string with NULL - Stack Overflow
So far I've declared these pointers ... the string conventionally so the rest of the program can use it. My question really is: When a keyword is not found, the relevant pointer will have not been correctly assigned, and therefore could be pointing anywhere. I've tried to solve this by initially setting it to NULL with ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to set element in array to null in C program - Stack Overflow
I am writing a C program in Unix and cannot figure out how to set an array element to NULL. I need to be able to do this to remove multiple characters from a string. More on stackoverflow.com
๐ŸŒ stackoverflow.com
c - Set char pointer to NULL after using in a function - Stack Overflow
In C, I have a function in which I am getting a string as a parameter and then after using it, I want to destroy it, because I have to call it in an infinite loop and getting Process returned -1073... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
YouTube
youtube.com โ€บ caleb curry
C Programming Tutorial 86 - Intro to Strings and Null Character - YouTube
Subscribe (Itโ€™s FREE!) - http://calcur.tech/subscribe (FREE) My C Programming Crash Course - http://calcur.tech/c-crash-course (FREE TRIAL) The C Programming...
Published: August 12, 2019
Views: 7K
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 21110346 โ€บ setting-a-string-with-null
c - setting a string with NULL - Stack Overflow
So far I've declared these pointers as char *, although I'm not certain if they shouldn't perhaps be void *, but in any case the code then scans forwards and substitutes the following linefeed character with a zero to terminate the string conventionally so the rest of the program can use it. My question really is: When a keyword is not found, the relevant pointer will have not been correctly assigned, and therefore could be pointing anywhere. I've tried to solve this by initially setting it to NULL with meta[which]=NULL;. This is further complicated because there are three of them.
Top answer
1 of 3
7

You can't assign null to specific char array index as value represented by that index is char instead of pointer. But if you need to remove specific character from given string, you can implement this as follows

void removeChar(char *str, char garbage) {

    char *src, *dst;
    for (src = dst = str; *src != '\0'; src++) {
        *dst = *src;
        if (*dst != garbage) dst++;
    }
    *dst = '\0';
}

Test Program

#include<stdio.h>
int main(void) {
    char* str = malloc(strlen("abcdef")+1);
    strcpy(str, "abcdbbbef");
    removeChar(str, 'b');
    printf("%s", str);
    free(str);
    return 0;
}

output

acdef
2 of 3
2

If you have a char[], you can zero-out individual elements using this:

char arr[10] = "foo";
arr[1] = '\0';

Note that this isn't the same as assigning NULL, since arr[1] is a char and not a pointer, you can't assign NULL to it.

That said, that probably won't do what you think it will. The above example will produce the string f, not fo as you seem to expect.

If you want to remove characters from a string, you have to shift the contents of the string to the left (including the null terminator) using memmove and some pointer arithmetic:

Example:

#include <stdio.h>
#include <string.h>

int removechars(char *str, size_t pos, size_t cnt) {
    size_t len = strlen(str);
    if (pos + cnt > len)
        return -1;

    memmove(str + pos, str + pos + cnt, len - pos - cnt + 1);
    return 0;
}

Then use it like so:

char str[12] = "hello world";
if (removechars(str, 5, 4) == 0)  /* remove 4 chars starting at str[5] */
    printf("%s\n", str);          /* hellold */
Find elsewhere
๐ŸŒ
Flavio Copes
flaviocopes.com โ€บ home โ€บ how to use null in c
How to use NULL in C - Flavio Copes
February 13, 2020 - They both have the value zero, but NULL is for pointers and '\0' is a char inside a string. A line like char *a_string = '\0'; compiles, but it does not create an empty string. It sets the pointer to NULL, because '\0' is just the integer zero.
๐ŸŒ
Quora
quora.com โ€บ How-do-you-initialize-char*-to-an-empty-string-in-C
How to initialize char* to an empty string in C - Quora
An empty string in C - meaning ... on strings - is simply "". It is an array of char with a element, and the value of that element is (char) 0 or '\0'; Confusingly, this character in the ASCII and UTF-8 character sets is also called NUL, which is different from the C NULL used for ...
๐ŸŒ
Northern Illinois University
faculty.cs.niu.edu โ€บ ~winans โ€บ CS501 โ€บ Notes โ€บ cstrings.html
C Strings
The individual characters that make up the string are stored in the elements of the array. The string is terminated by a null character. Array elements after the null character are not part of the string, and their contents are irrelevant. A "null string" or "empty string" is a string with a null character as its first character: The length of a null string is 0. ... This declaration creates an unnamed character array just large enough to hold the string "Karen" (including room for the null character) and places the address of the first element of the array in the char pointer name:
๐ŸŒ
Quora
quora.com โ€บ What-is-a-null-string-in-C
What is a null string in C? - Quora
Answer (1 of 2): The term โ€œnull stringโ€ is very ambiguous in a language like C. It might mean that you have a pointer to char which is set to NULL. In this case, there is no string at all. All you have is a pointer to NULL (a pointer that contains memory address 0). To create one of these, ...
๐ŸŒ
C For Dummies
c-for-dummies.com โ€บ blog
Null Versus Empty Strings | C For Dummies Blog
August 12, 2017 - A null string has no values. Itโ€™s an empty char array, one that hasnโ€™t been assigned any elements. The string exists in memory, so itโ€™s not a NULL pointer.
๐ŸŒ
Tutorial and Example
tutorialandexample.com โ€บ null-character-in-c
Null character in C - TAE
March 28, 2022 - In other words, the Null character is used to represent the end of the string or end of an array or other concepts in C. The end of the character string or the NULL byte is represented by โ€˜0โ€™ or โ€˜\0โ€™ or simply NULL. The NULL character doesnโ€™t have any designated symbol associated with it and also it is not required consequently.
๐ŸŒ
Quora
quora.com โ€บ How-do-you-put-null-characters-into-a-string-in-the-C-programming-language
How to put null characters into a string in the C++ programming language - Quora
Answer (1 of 3): For C++, I suggest std::string, and letting it deal with null chars. IMO, a very bad idea to write null chars into std::strings If you must use C style strings, and sometimes you must use them, also donโ€™t write null the strings as a general best practice. Reasons you might have...