Take the 2-minute tour ×

How can I make the following work? The idea is for the function to allocate an external pointer so I can use this concept in another program, but I can't do that because gcc keeps telling me that the argument is from an incompatible pointer type... It should be simple, but I'm not seeing it.

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

int allocMyPtr(char *textToCopy, char **returnPtr) {
    char *ptr=NULL;
    int size=strlen(textToCopy)+1;
    int count;

    if(NULL!=ptr) {
        for(count=0;count<size;count++) {
        *returnPtr = ptr;
        return 1;
    } else {
        return 0;

int main(void) {
    char text[]="Hello World\n";
    char *string;

    if(allocMyPtr(text,string)) {
    } else {
        printf("out of memory\n");
        return EXIT_FAILURE;
   return EXIT_SUCCESS;
share|improve this question
allocMyPtr(text,&string) –  Roddy Nov 27 '12 at 13:01
Forget the line in main that copy "text" to "string", it's unecessary –  dse Nov 27 '12 at 13:04
add comment

3 Answers

up vote 2 down vote accepted

It's almost correct, but as your function wants a pointer to a pointer, you have to pass the address of the pointer to the function, using the address-of operator:

allocMyPtr(text, &string)
share|improve this answer
Yeah, that's it hehe Sooo simple it hurts... –  dse Nov 27 '12 at 13:26
add comment

You are passing string using pass by value in allocMyPtr() you should use pass by adress so that pointer should match otherwise compiler keep tellin you about ,

incompatible type char * to char **

do this :

if(allocMyPtr(text,&string)) { }
share|improve this answer
add comment

use &string instead to fix your problem the type related to this input parameter is char ** and not char *

if(allocMyPtr(text,&string)) {

Just a remark concerning your source code:

The allocMyPtr() function already do a copy from text to string.

so why you make copy agian with strcpy. it's useless

strcpy(string,text); // this useless
share|improve this answer
add comment

Your Answer


