35

I want to initialize string in C to empty string. I tried:

string[0] = ""; 

but it wrote

"warning: assignment makes integer from pointer without a cast"

How should I do it then?

5
  • 4
    What's the point of declaring a 0-character string? Commented Nov 10, 2010 at 9:05
  • 1
    What is the type of your string ? Is it a char pointer or a char array ? Commented Nov 10, 2010 at 9:05
  • @cdhowie: It can be a useful sentinel value, for instance. Commented Nov 10, 2010 at 9:08
  • Oh, I misunderstood. "string" was a variable defined previously. I thought this was some sort of weird fubared variable declaration. Commented Nov 10, 2010 at 9:11
  • valid Question @leppie What is String. Commented May 23, 2012 at 6:55

9 Answers 9

46

In addition to Will Dean's version, the following are common for whole buffer initialization:

char s[10] = {'\0'};

or

char s[10];
memset(s, '\0', sizeof(s));

or

char s[10];
strncpy(s, "", sizeof(s));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot 8 years old answer. Couldn't find much on whole buffer initialization.
35

You want to set the first character of the string to zero, like this:

char myString[10];
myString[0] = '\0';

(Or myString[0] = 0;)

Or, actually, on initialisation, you can do:

char myString[10] = "";

But that's not a general way to set a string to zero length once it's been defined.

4 Comments

char myString[1] = ""; surely? You need space for the null terminator.
Correction: you could do char myString[10] = "";
@JeremyP: "" stands for "\0", so I think char myString[1] is enough.
@NanXiao The code said char myString[0] = ""; when I wrote the comment. It has since been corrected.
21

Assuming your array called 'string' already exists, try

string[0] = '\0';

\0 is the explicit NUL terminator, required to mark the end of string.

1 Comment

@badgerr: would you please change your answer to use '\0' ?
5

Assigning string literals to char array is allowed only during declaration:

char string[] = "";

This declares string as a char array of size 1 and initializes it with \0.

Try this too:

char str1[] = ""; 
char str2[5] = ""; 
printf("%d, %d\n", sizeof(str1), sizeof(str2)); //prints 1, 5

Comments

4

calloc allocates the requested memory and returns a pointer to it. It also sets allocated memory to zero.

In case you are planning to use your string as empty string all the time:

char *string = NULL;
string = (char*)calloc(1, sizeof(char));

In case you are planning to store some value in your string later:

char *string = NULL;
int numberOfChars = 50; // you can use as many as you need
string = (char*)calloc(numberOfChars + 1, sizeof(char));

Comments

4

To achieve this you can use:

strcpy(string, "");

Comments

3
string[0] = "";
"warning: assignment makes integer from pointer without a cast

Ok, let's dive into the expression ...

0 an int: represents the number of chars (assuming string is (or decayed into) a char*) to advance from the beginning of the object string

string[0]: the char object located at the beginning of the object string

"": string literal: an object of type char[1]

=: assignment operator: tries to assign a value of type char[1] to an object of type char. char[1] (decayed to char*) and char are not assignment compatible, but the compiler trusts you (the programmer) and goes ahead with the assignment anyway by casting the type char* (what char[1] decayed to) to an int --- and you get the warning as a bonus. You have a really nice compiler :-)

2 Comments

Would get more upvotes if it had a solution; this is a good explanation of what's happening though.
I agree @wizzwizz4, but I like my answer as it is. Thanks!
0

I think Amarghosh answered correctly. If you want to Initialize an empty string(without knowing the size) the best way is:

//this will create an empty string without no memory allocation. 
char str[]="";// it is look like {0}

But if you want initialize a string with a fixed memory allocation you can do:

// this is better if you know your string size.    
char str[5]=""; // it is look like {0, 0, 0, 0, 0} 

Comments

0

It's a bit late but I think your issue may be that you've created a zero-length array, rather than an array of length 1.

A string is a series of characters followed by a string terminator ('\0'). An empty string ("") consists of no characters followed by a single string terminator character - i.e. one character in total.

So I would try the following:

string[1] = ""

Note that this behaviour is not the emulated by strlen, which does not count the terminator as part of the string length.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.