char str1[20] = "Something";
char *str2 = "Random";
strcat(str1, str2[1]);
It gives the error pointer from integer without a cast
why is str2[1] treated as an integer?
so what should I do if I want to strcat individual elements in a string
Thanks!
Because an element from a char array is a char, which is an integral type. If you want a pointer to the second element, try str2 + 1 or &str2[1].
With str2[1] the 2nd character is meant (starting at index 0), which is the 'a' in 'Random'.
If you want the complete string you need to write str2 instead of str2[1].
strcat is expecting pointers to chars, but str2[1] is a char value,
modify it to be &str2[1] to get back a pointer to the second char in str2.
str2[1] is of type char while in the strcat(), a char * is expected. Here you should have used str2 + 1.
str2[1] is a char which is promoted to an int according to C type promotion rules. If you want to append a single character to a string, try the following:
int len = strlen(str1);
str1[len] = str2[1];
str1[len + 1] = '\0';
In other words, strlen only concatenates two strings, not a string and a character.
Related
I have a string,
char* str = "HELLO"
If I wanted to get just the E from that how would I do that?
char* str = "HELLO";
char c = str[1];
Keep in mind that arrays and strings in C begin indexing at 0 rather than 1, so "H" is str[0], "E" is str[1], the first "L" is str[2] and so on.
You would do:
char c = str[1];
Or even:
char c = "Hello"[1];
edit: updated to find the "E".
Array notation and pointer arithmetic can be used interchangeably in C/C++ (this is not true for ALL the cases but by the time you get there, you will find the cases yourself). So although str is a pointer, you can use it as if it were an array like so:
char char_E = str[1];
char char_L1 = str[2];
char char_O = str[4];
...and so on. What you could also do is "add" 1 to the value of the pointer to a character str which will then point to the second character in the string. Then you can simply do:
str = str + 1; // makes it point to 'E' now
char myChar = *str;
I hope this helps.
This code doesn't work.
char* randomWordHidden[length];
char try;
printf("Enter a letter: ");
scanf(" %c", &try);
//here there is a loop
randomWordHidden[i] = try; //assign position of randomWordHidden with try value
it gives error: [Warning] assignment makes pointer from integer without a cast
But I do this and it works:
randomWordHidden[i] = "H";
How can i assign to a position of randomWordHidden the value of try var?
Well;
you have made a list (of length length) of pointers to characters. I guess what you want is:
char randomWordHidden[length];
that should be length characters.
Your randomWordHidden is not an array of char, but an array of char*, so effectively an array of strings. That's why assigning a char gives you a warning, because you cannot do char* = char. But the assignment of "H", works, because it is NOT a char - it is a string (const char*), which consists of letter 'H' followed by terminating character '\0'. This is char - 'H', this is string (char array) - "H".
You most likely need to change the declaration of the randomWordHidden array to char instead of char*.
I believe arrays are already pointers, no need for that declaration.
char randomWordHidden[length];
Give that a try?
I'm wondering what the difference is between
char* str[NUM];
char str[NUM];
char* str;
My understanding is that an array definition such as the one in the middle points to the address of the first value, that is str[0]. But I also see the notation represented by the first line of code, that is, a pointer to an array. Are these three equivalent? I'm aware the array definition sets space in the stack for NUM characters so is that probably better than just
char* str?
Thanks
char* str[NUM];
This is an array of NUM char pointers
char str[NUM];
This is an array named str which is NUM chars long.
char *str;
This is a pointer to type char
char* str[NUM];
Declares str as array of num pointers to chars.
char str[NUM];
Declares str as array of num chars.
char* str;
Declares str as pointer to a char.
You can test these type of declarations here.
char* str[NUM];
An Array with NUM elements. Each element is a pointer-to-character(s).
In common terms, this is likely an array of strings.
str[0] is the first string.
str[1] is the second string, etc, etc.
Any element of this array may be NULL.
char str[NUM];
An array with NUM elements. Each element is an individual character.
In common terms, this is a fixed-size string of length NUM-1.(the last position is for the string null-terminator)
This string cannot be NULL. It can be empty (""), but it always exists.
char* str;
A pointer to characters(s).
In common terms, this is a string, of no particular fixed size. You'll find the end of the string when you find the null-terminator.
This string can be NULL, and not point to any memory.
char* str1[NUM]; // an array of size NUM of pointers to char
char str2[NUM]; // an array of size NUM of char
char* str3; // a pointer to char
Uses of these might be:
str1[NUM-1] = strdup("foo");
strcpy(str2, "foo");
str3 = strdup("foo");
I'm trying to terminate a string early based on some arbitrary condition, by doing this:
void end_string_early(char string[], int len) {
int i;
char j;
for (i=0;i<len;i++) {
j = string[i];
if (arbitrary_condition(j)) {
string[i] = "a";
}
}
}
I get the compile error:
warning: assignment makes integer from pointer without a cast
What confuses me, is if I do the exact same thing with an int array (changing the value to be another int, instead of a) then it works perfectly. Perhaps it's something with how the argument is being passed? (Though I was under the impression all arrays were passed by ref) I'm not entirely sure and this is my first go at C (working through K&R book).
'a' and "a" are two different things: the first is the character a, the second is a literal string containing only the character a.
You're trying to assign a literal string to a character. "a" is a literal string, i.e. a char *, and an element of your char[] is a char: an integral type, hence the message about trying to assign a pointer to an integer. The message is a bit confusing because char is an integral type.
So use:
string[i] = 'a';
But this doesn't terminate the string. For that you'll need:
string[i] = '\0';
string[i] = "a";
must be
string[i] = 'a';
Note the different quotes.
string is an array from chars, and "a" is not a char, it's string literal, containing two chars - one for a and one \0.
So, to set a char element into a char array, you need to use single quotes '.
Your program will work better with string[i] = 'a'; (instead of "a").
Just in case, be aware that it's not possible to re-assign the chars of a literal string. The piece of code you show does not have this problem, but if you called end_string_early("foo"...), it would.
"a" is not a char -- it's a char* (string). You need 'a'.
string[i] = "a" should be string[i] = 'a', "a" is different from 'a', where "a" is a literal string and it "returns a pointer, while 'a' is just a char.
"a" is a char[2], which decays to a char* and that's why you're getting that warning; string is a char* so string[i] is a char which means you are trying to assign a char* to a char, which is wrong.
You need to use single quotes:
string[i] = 'a';
Double quotes make an array of char and single quotes make just a single char, so 'a' is a char and assigning a char to a char will work.
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?
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));
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.
Assuming your array called 'string' already exists, try
string[0] = '\0';
\0 is the explicit NUL terminator, required to mark the end of string.
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
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));
To achieve this you can use:
strcpy(string, "");
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 :-)
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}
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.