Why can't I do strcpy?
Asked Answered
E

3

5
#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?

Eyre answered 14/5, 2018 at 21:38 Comment(8)
Neither str[i] nor hello[i] is a pointer -- both are characters. And the next problem is that str[14] is off the end of the allocated memory -- you've allocated 14 bytes, with valid indexes from 0 to 13.Localism
str is a pointer, str[i] is not.Rivulet
strcpy(str, hello) is the correct invocation.Localism
... without the loop.Zabrze
I forget that strcpy copies the null character, thanksEyre
char *str = strdup(hello); copies the string without needing to worry about counting bytes.Localism
@PaulHankin strdup is not standard.Europe
@MichaelWalz it's in the POSIX standardOverwind
U
6

You are doing it wrong:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main() {
   const char* hello = "Hello, World!";
   char *str = malloc(strlen(hello)+1);

   strcpy(str,hello);
   printf("%s\n", str);
   free(str);
   return 0;
}

Explanation: strcpy operates on pointers, where both are start location to write to and read from, so you have to pass those, not characters. Your read location is hello, your write location is str. Then strcpy loops until it finds a 0 character (which is included) to stop copying, so your loop is unnecessary. Last thing is that you have to free allocated memory. Also sizeof(char) doesn't make sense: it's always 1.

Undis answered 14/5, 2018 at 21:46 Comment(2)
You present a correct implementation, but this would be a much better answer if it also explained why the OP's code is wrong, and what the error messages mean.Zabrze
Agree just added that, maybe that will help understand the problemUndis
L
2

The issue here is your attempting to use C-strings as arrays of characters, which is certainly allowed but it's a different behavior than using them as pointers to null-terminated strings. Doing hello[0] evaluates to the first character of the string, which is simply a usually 8-bit integer. A char is a value, it does not correspond to a memory address. The correct statement you want is

strcpy(str, hello);

For reference, if you want to get a string starting at some point in your string, you would do

strcpy(str, hello + 1);

Performing addition on a pointer evaluates to a pointer that is some n addresses forward in memory.

Levi answered 14/5, 2018 at 21:46 Comment(4)
You might want to delete references to pointer arithmetic - it's probably off-topic in this answer, and may be generally confusing.Labialize
deleted it, but i thought the asker was attempting to get the ith point where a string starts by mistakenly doing str[i] in a function that accepts a pointer.Levi
hello[0] evaluates a char which is usually 8-bit (but may not be) and can be signed or unsigned. Only some chars are printable characters (specifically the non-negative ones for which isprint(c) returns non-zero). I think your answer is in spirit correct, but these small technical errors subtract from its quality.Localism
@PaulHankin good point, I fixed it just to clarify that a char is a value and not an address of memory or a pointer.Levi
I
0

The definition of strcpy takes two char pointers not the str[], hello[] arrays.

char *strcpy(char *destination, const char *source)
Impearl answered 14/5, 2018 at 21:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.