I've a function which receives a const char* and I want to convert it to lowercase. But I get the error:
error: array initializer must be an initializer list or string literal
I tried to copy the string variable to another array so that I could lower case it. But I think I've got something confused.
This is my function:
int convert(const char* string)
{
char temp[] = string;
temp = tolower(temp); //error is here
//do stuff
}
I'm struggling to understand what this error means, could someone help in explaining it?
tolower takes a single character and returns it in lowercase.
Even if it didn't, arrays aren't assignable. Arrays and pointers are not the same thing.
You presumably want to do something like:
char *temp = strdup(string); // make a copy
// adjust copy to lowercase
unsigned char *tptr = (unsigned char *)temp;
while(*tptr) {
*tptr = tolower(*tptr);
tptr++;
}
// do things
// release copy
free(temp);
Make sure you understand the difference between the heap and the stack, and the rules affecting string literals.
First of all, tolowertakes char or int, not string.
but even if you passed char, your code wouldn't work, because this error array initializer must be an initializer list or string literal ,means you have to initialize it using one of the following methods:
char arr[4] = {'h', 'e', 'y','\0'}; // initializer list
char arr[4] = "hey"; // string literal
char arr[] = "hey"; // also a string literal
char arr[4];
arr[0] = 'h';
arr[1] = 'e';
arr[2] = 'y';
arr[4] = '\0';
Please note that, tolower() takes a character and not a string:
int tolower ( int c );
Also you are trying to copy the string from string to temp[] variable. There in no overloading of the = operator in C to copy the string into char array.
Now about your error:
error: array initializer must be an initializer list or string literal
It says that the array must be initialized via an list of initial individual items or you can directly assign a string literal.
E.g:
char temp[] = "Hi";
char temp[] = {'H','i','\0'};
Also, check your statement:
temp = tolower(temp);
Here the return type of tolower() is int that you are assigning it to the array temp which is improper.
You can use the following snippet using strlwr():
int convert(const char* string)
{
char *temp= malloc(strlen(string)+1);
strcpy(temp,string);
strlwr(temp);
//do your stuff
}
Related
I have a question regarding my code here:
char a = 'A';
char b = 'B';
char c = 'C';
char *ad = &a;
char *ab = &b;
char *ac = &c;
char* cp[] = {ad, ab, ac};
char **d = &(cp[2]); // okay
// char **d[0] = &(cp[2]); //variant 1: error: invalid initializer
// char **d[] = &(cp[2]); //variant 2: error: invalid initializer
I am not sure why variant1 and variant2 would leads to error. For the original initialization (i.e. the line with comment okay), there was no error. My understanding is I have initialize a pointer (i.e. d) which is pointing to a pointer that pointing to a character. So this seems fine.
For variant1, I thought (based on my understanding) I am initializing an array of pointers where each of them will point to a pointer that points to a character. So in this case, I only initialize the very first element in that array.
Similarly for variant2, I initialize an empty array of pointers that each would points to a character.
Could someone tells me why there is error of coming from the compiler here?
And is my understanding of variant 1 correct? i.e. d is an array of pointers where each entry in the array would points to a pointer that points to a character??
I thought this is correct. So why is it that I cannot initialize variant 1?
For variant2, I also thought that I have an array of pointers each pointing to a pointer that points to a character.
This compiles cleanly...
char a = 'A';
char b = 'B';
char c = 'C';
char *ad = &a;
char *ab = &b;
char *ac = &c;
char *cp[] = {ad, ab, ac}; // be consistent
char **d = &cp[2];
char **e[1] = { &cp[2] }; // cannot dimension an array to have 0 elements
char **f[] = { &cp[2] }; // compilers count more accurately than people
Note the absence of unnecessary "()".
Array initialisers are listed within enclosing braces. "{}"
Exception to last statement: char foo[] = "bar";... "string" array elements do not require braces unless one gets silly:
char foo[] = { 'b', 'a', 'r', '\0', };
Generally, you can initialize a pointer with any string literals like char *str = "Hello". I think this means "Hello" returns the address of 'H'. However, the below isn't allowed.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char name[64];
} Student;
Student initialization(char *str) {
//Student tmp = {}; strcpy(tmp.name, str) //(*1)This is allowed.
//Student tmp = {"Hello"}; //(*2)This is allowed.
Student tmp = {str}; //(*3)This is not allowed.
return tmp;
}
int main(void) {
(...)
}
Could anyone tell me the reason why (*2) is allowed but (*3) is not allowed? Compiling this code makes the error below.
warning: initialization makes integer from pointer without a cast [-Wint-conversion]
Student tmp = {str};
^
All these cases you are trying to initialize a char array. Now after saying that - we can see it makes thing easier. Just like an char array where if we write down a string literal directly it initializes the char array with the content of the string literal.
But in the second case, the string literal which is basically a char array is converted to a pointer to the first element of it (the fist character of string literal) which is then used to initialize the char array. That will not work. Note that, even if str is a pointer to a char array which is not a literal this won't work. For the same reason as specified. Standard allows initialization from the string literal directly. Not other way round.
From standard 6.7.9p14
An array of character type may be initialized by a character string literal or UTF-8 string literal, optionally enclosed in braces. Successive bytes of the string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.
I'm new to C and am having trouble declaring a character array for a class project.
Here's roughly what I'm doing:
char test[]="Test\0";
char *Pointer;
Pointer=test;
I then have a function printString(char* chars) that takes Pointer as an argument. When I try to compile, I'm told that neither test or Pointer are declared. I'm not sure why, so can someone point me in the right direction.
This is the whole code:
main()
{
char *test2="Test\0";
printString(test2);
}
printString(char* charArray)
{
int charPos=0;
int endOfString=1;
char al;
char ah;
int ax;
while(endOfString==1)
{
al=charArray[charPos];
ah=0xE;
ax=ah*256+al;
interrupt(0x10,ax,0,0,0);
if(al=='\0')
{
return 0;
}
charPos++;
}
}
First of all, having the NULL character is not necessary there.
Here is why:
When an array of characters is declared and it is initialize, like in your example:
char test[] = "Test";
The compiler will put the characters from "Test" in the test array, then add a null character so that test can be used as a string like so:
+---+---+---+---+----+
test| T | e | s | t | \0 |
+---+---+---+---+----+
In regards to your question, see if this helps:
void print_string( char *p ) {
while ( *p != '\0' ) {
printf( "%c", *p );
p++;
}
}
Remember a C-style string is a sequence of characters and it's always terminated with NULL character.
The function "print_string", for example, expects a pointer to a char as an argument ( you can past the char array you created, and it will work since arrays are treated as pointers. )
The function will print each character until the NULL character is encountered.
IMPORTANT:
char test[] = "Test";
char *test_2 = "Test";
In the array version, the characters stored in test can be modified, like elements of any array.
In the pointer version, test points to a string literal, and string literals should NOT be modified.
I believe the problem might be because of that, you are trying to modified a string literal. Doing so causes undefined behavior.
Either use an array or allocate memory and make char pointer point to it.
For use your function: printString(char* chars).
declare just:
char *my_string = "Test";
printString(my_string);
The problem is possibly because you don't declare the function before you use it. In C you really have to declare everything before it's used. In this case do e.g.
void printString(char* charArray);
int main(void)
{
...
}
void printString(char* charArray)
{
...
}
What is the difference between
char ch [ ] = "hello";
and
char ch [ ] = { 'h','e','l','l','o','\0'};
and why we can only do
char *p = "hello";
But cant do
char *p = {'h','e','l','l','o','\0'};
char ch [ ] = "hello";
char ch [ ] = { 'h','e','l','l','o','\0'};
There is no difference. ch object will be exactly the same in both declarations.
On why we cannot do:
char *p = {'h','e','l','l','o','\0'};
An initializer list of more than one value can only be used for objects of aggregate type (structures or array). You can only initialize a char * with a pointer value.
Actually:
char ch [ ] = "hello";
and
char *p = "hello";
are not the same. The first initializes an array with the elements of a string literal and the second is a pointer to a string literal.
The two array declarations are the same. As for the pointer declaration, the form char *p = {'h','e','l','l','o','\0'}; is not valid, simply because it was not included in the compiler design.
There is no theoretical reason, in my knowledge, why that declaration should not be valid, when char *p = "hello"; is.
Open upon a time they made a mistake about const
The bit "hello" should be a const char * const but they where lazy and just used char *. But to keep the faith alive they let that one slip.
Then they said. Ok. They can be equal.
Then they had char [] = { 'a', 'b', ...}; and all was good in the world
The the evil monster came and thrust upon them char *p = "hello". But the evil monster was in a good mood and said it should be const char *p = "hello" but I would be happy with that.
He went home and but the evil monster was not amused. He dictated over his realm char *p = {'h','e','l','l','o','\0'}; is the sign of a heretic.
Basically there was a cock-up. Just do it right from now and there is old code kicking around that needs to be satisfied.
Let's take it one by one. This following is initializing a char array ch using the string literal "hello".
char ch[] = "hello";
The following is initializing the array ch using an array initialization list. This is equivalent to the above statement.
char ch[] = {'h', 'e', 'l', 'l', 'o', '\0'};
The following is initializing a char pointer p to point to the memory where the string literal "hello" is stored. This is read-only memory. Attempting to modify its contents will not give compile error because string literal in C are not const qualified unlike in C++, but will cause undefined behaviour or even program crash.
char *p = "hello";
const char *p = "hello"; // better
The following last statement is plain wrong.
char *p = {'h','e','l','l','o','\0'};
p is a char pointer here, not an array and can't be initialized using array initialization list. I have highlighted the words array and pointer above to emphasize that array and pointer are different types. In some cases, an array is implicitly converted to a pointer to its first element like when an array is passed to a function or assigned to a pointer of the same type. This does not mean they are the same. They have different pointer arithmetic and different sizeof values.
There is no difference in
char ch [ ] = "hello";
char ch [ ] = { 'h','e','l','l','o','\0'};
But check below
char *p = "hello"; //This is correct
char *p = {'h','e','l','l','o','\0'}; //This is wrong
If you want to make it correct you need to use
char *p[]={'h','e','l','l','o','\0'}; //This works
Why is the following code illegal?
typedef struct{
char a[6];
} point;
int main()
{
point p;
p.a = "onetwo";
}
Does it have anything to do with the size of the literal? or is it just illegal to assign a string literal to a char array after it's declared?
It doesn't have anything to do with the size. You cannot assign a string literal to a char array after its been created - you can use it only at the time of definition.
When you do
char a[] = "something";
it creates an array of enough size (including the terminating null) and copies the string to the array. It is not a good practice to specify the array size when you initialize it with a string literal - you might not account for the null character.
When you do
char a[10];
a = "something";
you're trying to assign to the address of the array, which is illegal.
EDIT: as mentioned in other answers, you can do a strcpy/strncpy, but make sure that the array is initialized with the required length.
strcpy(p.a, "12345");//give space for the \0
You can never assign to arrays after they've been created; this is equally illegal:
int foo[4];
int bar[4];
foo = bar;
You need to use pointers, or assign to an index of the array; this is legal:
p.a[0] = 'o';
If you want to leave it an array in the struct, you can use a function like strcpy:
strncpy(p.a, "onetwo", 6);
(note that the char array needs to be big enough to hold the nul-terminator too, so you probably want to make it char a[7] and change the last argument to strncpy to 7)
Arrays are non modifiable lvalues. So you cannot assign to them. Left side of assignment operator must be an modifiable lvalue.
However you can initialize an array when it is defined.
For example :
char a[] = "Hello World" ;// this is legal
char a[]={'H','e','l','l','o',' ','W','o','r','l','d','\0'};//this is also legal
//but
char a[20];
a = "Hello World" ;// is illegal
However you can use strncpy(a, "Hello World",20);
As other answers have already pointed out, you can only initialise a character array with a string literal, you cannot assign a string literal to a character array. However, structs (even those that contain character arrays) are another kettle of fish.
I would not recommend doing this in an actual program, but this demonstrates that although arrays types cannot be assigned to, structs containing array types can be.
typedef struct
{
char value[100];
} string;
int main()
{
string a = {"hello"};
a = (string){"another string!"}; // overwrite value with a new string
puts(a.value);
string b = {"a NEW string"};
b = a; // override with the value of another "string" struct
puts(b.value); // prints "another string!" again
}
So, in your original example, the following code should compile fine:
typedef struct{
char a[6];
} point;
int main()
{
point p;
// note that only 5 characters + 1 for '\0' will fit in a char[6] array.
p = (point){"onetw"};
}
Note that in order to store the string "onetwo" in your array, it has to be of length [7] and not as written in the question. The extra character is for storing the '\0' terminator.
No strcpy or C99 compund literal is needed. The example in pure ANSI C:
typedef struct{
char a[6];
} point;
int main()
{
point p;
*(point*)p.a = *(point*)"onetwo";
fwrite(p.a,6,1,stdout);fflush(stdout);
return 0;
}