Hi! what is wrong with the following function?

void foo(*ptr){
char buff[127];
ctrcpy(*ptr,buff);
}

I know that it doesn't really do something... it's for information purpose only....
Thanx!!

Recommended Answers

All 7 Replies

Have you tried to compile the code? That should give you a list of errors telling you some of the things wrong with that code.

use strcpy instead of ctrcpy

Also, you need to declare the type in the function argument list. This should not compile. And it would core dump since buff[] is uninitialized.

sorry.. bunch of typo errors...
it scould be something like this:

void foo(char* ptr){
char buff[127];
strcpy(*ptr,buff);
}

That should still produce a type error when you try to compile it.

You did not explain what you're trying to do here (or how exactly your real code does not work - you seem to be implying that your real code does compile), so it's not clear to me what you're trying to copy where. If you intend to copy into buff, you have your argument mixed up. If you intend to copy out of buff, rubberman is right that you need to initialize it before you use it - otherwise what exactly do you think you're copying?

Also if you intend to copy into buff one obvious problem is that you never do anything with buff before foo ends.

Another potential problem is your use of strcpy, which will lead to a buffer overrun if the given string is too large to fit into the buffer. You should use strncpy instead.

Can't we assign some memory to ptr and then copt string to it ?
because we dont't have expilict memory available.

void foo(char* ptr) ;
int main()

void foo(char* ptr) ; 
int main()
{
    char* ptr ; 
    char buff[127] = "helloworld";
   // ptr= buff ;
    ptr = (char *)malloc(127) ; 

    strcpy(ptr,buff);

    for(int i = 0 ; i < 10 ; i++)
    {
    printf("%c",*ptr);
        ptr++ ;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.