1

I'm trying to pass two strings to a function that copies the second string into the first string. Get the error messages

"passing arguement 1/2 of 'CopyStrings'from incompatible pointer type - line 25.
note: expected 'char*' but argument is of type 'char**' - line 8

Here is my code:

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

// a function that gets 2 strings and copies the second one into the first one


char CopyStrings(char* string1, char* string2)
{
  strcpy(string1, string2);

  return *string1;

}

int main()
{
  char* string1 = (char*)malloc(sizeof(char)*10);
  char* string2 = (char*)malloc(sizeof(char)*10);

  strcpy(string1, "ugabuga");
  strcpy(string2, "mukaluka");


  printf("%s", CopyStrings(string1, string2));


  free(string1);
  free(string2);
  return 0;

}

Any suggestions what I messed up and how I write the pointers correctly?

EDIT - Now that I changed

(&string1, &string2)

into

(string1, string2)

the program chrashes.

Yíu
  • 383
  • 3
  • 14
  • Don't cast `malloc` in C. Also, you obviously are asking for the address of the `char *` by doing `&string1` in the function call, which passes a `char **`. – crashmstr Jan 21 '15 at 15:21
  • Ah true. Can you explain why I should not use malloc here? – Yíu Jan 21 '15 at 15:30
  • use malloc != cast malloc. Casting malloc is not needed and iirc it suppresses some kind of warning, but I'm not exactly sure. It's discouraged. – MightyPork Jan 21 '15 at 15:34
  • @Yíu You shouldn't *cast* the result from `malloc`, as it will make your code compile (and probably crash badly) if you forgot to include its header. (It's unnecessary to use too in this case, as you can just write `char string1[10];` and get rid of `free`). – molbdnilo Jan 21 '15 at 15:35
  • Hmm okay. But in this example I think casting malloc is okay as the program runs fine if I exclude the function /* and just printf my strings. – Yíu Jan 21 '15 at 15:40
  • [Please don't cast the result of `malloc()` for the reasons herein listed](http://stackoverflow.com/a/605858/3233393). – Quentin Jan 21 '15 at 16:27

3 Answers3

3

You really want just

CopyStrings(string1, string2)

It's char* already, and you're making char** from it with the ampersand.

Another problem is in your function - it should be:

char* CopyStrings(char* string1, char* string2)
{
    strcpy(string1, string2);
    return string1;
}

(if you want to return the first string).

MightyPork
  • 18,270
  • 10
  • 79
  • 133
1

Change

CopyStrings(&string1, &string2)

to

CopyStrings(string1, string2)

char* string1, here string1 is a pointer and when you pass address if pointer which is pointer to pointer, its not what you mean and formal arguments should be double pointers. Passing just the pointer is sufficient.

The CopyStrings() function need to return a char * string after performing strcpy() to caller. Here is the modified code.

Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
1

There were several problems with the OPs code:

In the following code, most of the problems are commented (and fixed) One thing to know, in C, an array name degrades to the address of the array try to always pass pointer to array rather than the complete array the %s conversion operator is expecting the associated parameter to be the address of a char array.

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

// a function that gets 2 strings and copies the second one into the first one


char* CopyStrings(char* string1, char* string2)
{
  strcpy(string1, string2);
  return string1; // return pointer to string1
}

int main()
{
  char* string1 = malloc(10);  // char is always 1 so no need for sizeof(char)
  char* string2 = malloc(10);

  strcpy(string1, "ugabuga");
  strcpy(string2, "mukaluka");

  printf("%s\n", CopyStrings(string1, string2));

  free(string1);
  free(string2);
  return 0;
} // end function: main
Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • Thanks for the help, appreciate. But since a char pointer is equal to an array it kinda does make sense to allocate memory for it - or am I wrong? – Yíu Jan 21 '15 at 21:04
  • @Yiu you're right. Whether it is char array or pointer to char, memory allocation is must but first case can be compilation time and later one allocates at runtime. – Sunil Bojanapally Jan 22 '15 at 05:14