#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main() {
const char* hello = "Hello, World!";
char *str = malloc(14 * sizeof(char));
for (int i = 0; i < 14; i++) {
strcpy(str[i],hello[i]);
}
str[14]='\0';
printf("%s\n", str);
return 0;
}
Compilation warnings:
warning: passing argument 1 of 'strcpy' makes pointer from integer without a cast [-Wint-conversion] note: expected 'char *' but argument is of type 'char' warning: passing argument 2 of 'strcpy' makes pointer from integer without a cast [-Wint-conversion]
str is a pointer and hello too, what's going on?
str[i]norhello[i]is a pointer -- both are characters. And the next problem is thatstr[14]is off the end of the allocated memory -- you've allocated 14 bytes, with valid indexes from 0 to 13. - Paul Hankinstris a pointer,str[i]is not. - tkauslstrcpy(str, hello)is the correct invocation. - Paul Hankinstrdupis not standard. - Jabberwocky