Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I do strcpy?

Tags:

c

pointers

strcpy

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

like image 383
jhonnna Avatar asked May 14 '18 21:05

jhonnna


People also ask

Why is strcpy not working?

Your arrays aren't being enough to hold the string you're trying to store. String in C are defined as a null terminated sequence of characters, meaning that a proper string has a null byte at the end.

What can I use instead of strcpy?

The strncpy() and strncat() functions are similar to the strcpy() and strcat() functions, but each has an additional size_t parameter n that limits the number of characters to be copied. These functions can be thought of as truncating copy and concatenation functions.

Why is strcpy unsafe in C++?

strcpy has no way of knowing how large the destination buffer is (i.e. there is no length parameter) so sloppy programming using it can lead to overrunning the buffer and corrupting other memory. Such an overrun can lead to crashes, odd behaviour and may be exploitable by malware authors.

How do I use strcpy?

The syntax of the strcpy() function is: Syntax: char* strcpy (char* destination, const char* source); The strcpy() function is used to copy strings. It copies string pointed to by source into the destination .


2 Answers

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.

like image 61
Mateusz Wojtczak Avatar answered Sep 22 '22 08:09

Mateusz Wojtczak


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.

like image 37
Josh Weinstein Avatar answered Sep 20 '22 08:09

Josh Weinstein