Good day!
I have a function which takes a string
and converts all lowercase to uppercase. The problem lies in main
why it works in one way and the other way in which they ask me (without initializing a variable on the same line) does not work for me. Meft_strupcase.c
char *ft_strupcase(char *str)
{
int i;
i = 0;
while (str[i] != '\0')
{
if (str[i] >= 'a' && str[i] <= 'z')
str[i] -= 32;
i++;
}
return (str);
}
And mymain.c
#include <stdio.h>
char *ft_strupcase(char *str);
int main(void)
{
//char str1[] = "I made it"; De esta manera me funciona*
char str1[100];
str1[100] = "I made it";
printf("The sentence is %s\n", ft_strupcase(str1));
}
It throws me the following error.
main.c:11:12: warning: assignment makes integer from pointer without a cast [-Wint-conversion] str1[100] = "I made it";
I've been reading and it seems that it has to do with that I assign an integer to the pointer without invoking it but I can't get it out. Thank you!
If you have already declared a character array, and have not initialized it in the declaration , you will have to copy the string using the function or
strncpy
astring.h
similar function such asstrlcpy
orsnprintf
.I give you an example with strncpy:
The first parameter of strncpy is the destination string, the second the source string, and the third parameter the number of characters to copy.
What you can't do is this:
What you are saying there is to assign a character string at position 100 (we are wrong because the array would go from 0 to 99), but str1[100] is a character and you cannot assign a string to a character. And even if I let you, you would be overflowing the array because in the array of 100 you would be concatenating the 9 or 10 characters of your string.
This from here:
It works for you because you are assigning a value to it during the declaration.
EDIT: Answering the question in the comment, yes, if you go character by character you can do it, keep in mind that if you do it like this you have to use single quotes, since you assign characters instead of strings.