Hi im trying to allocate memory using malloc(). Also, im trying to make a copy of the string before i allocate it. So im using strcpy(). But im still having issues with compiling it. please help!!

#include <stdlib.h>
#include "setup.h"
     
char *run( const char * const str ){ 
          strcpy(str) = (char * const str)malloc(sizeof(char));
     
     return NULL;
}

Sorry, what are you trying to do????

Why would you want to copy the string before allocating it? Generally the way round to do things is allocate the memory for an object and then copy the data into the object.

You can not assign to the return value of strcpy or any other function in C.

strcpy takes 2 arguments, destination and source.

When you allocate through malloc you need to allocate enough memory for the object you want to hold. You have allocate sizeof(char) or 1 byte but you want to hold a string so you need at least the length of the string plus 1 (for the terminator) bytes.

Your function always returns NULL, if the rest of if worked the pointer to the allocated memory would be lost resulting in a memory leak.

Edited 6 Years Ago by Banfa: n/a

Sorry, what are you trying to do????

Why would you want to copy the string before allocating it? Generally the way round to do things is allocate the memory for an object and then copy the data into the object.

You can not assign to the return value of strcpy or any other function in C.

strcpy takes 2 arguments, destination and source.

When you allocate through malloc you need to allocate enough memory for the object you want to hold. You have allocate sizeof(char) or 1 byte but you want to hold a string so you need at least the length of the string plus 1 (for the terminator) bytes.

Your function always returns NULL, if the rest of if worked the pointer to the allocated memory would be lost resulting in a memory leak.

ok thanks for your advice. Im new to C so im taking it one step at a time. so pertaining to the malloc function. I modified it a little bit and ended up with this(still an error):

char *run( const char * const str ){ 
         str = (char *)malloc(sizeof(char * strlen +1));
     
     return NULL;
}

Break it into steps.

char *run( const char * const str ) {
    char *result = malloc( strlen(str) + 1 );
    if ( result != NULL ) {
        strcpy( result, str );
    }
    return result;
}

It's better than this, which does not check for errors.

strcpy( malloc( strlen(str) + 1 ), str );

You don't gain anything by busting a gut to make super-compressed code.

Break it into steps.

char *run( const char * const str ) {
    char *result = malloc( strlen(str) + 1 );
    if ( result != NULL ) {
        strcpy( result, str );
    }
    return result;
}

It's better than this, which does not check for errors.

strcpy( malloc( strlen(str) + 1 ), str );

You don't gain anything by busting a gut to make super-compressed code.

Thanks!. I tried this and i get an error : invalid conversion from void to char.

Then you are probably compiling as C++ rather than C.

What is the file name?
What compiler command line/IDE are you using?

Then you are probably compiling as C++ rather than C.

What is the file name?
What compiler command line/IDE are you using?

No im compiling in C. Im using Dev C

No im compiling in C. Im using Dev C

I ried this and it compiled:

char *result = (char *)malloc( strlen(str) + 1 );    
          if ( result != NULL ) {       
          strcpy( result, str );
              }    
              return result;
}

> Thanks!. I tried this and i get an error : invalid conversion from void to char.
This pretty much means you're compiling C++, despite what you say.
void* to char* is a SILENT conversion in C.

What does this print?

int main ( ) {
#ifdef __cplusplus
    printf("This is C++\n" );
#else
    printf("This is C\n" );
#endif
    getchar();
    return 0;
}

> Thanks!. I tried this and i get an error : invalid conversion from void to char.
This pretty much means you're compiling C++, despite what you say.
void* to char* is a SILENT conversion in C.

What does this print?

int main ( ) {
#ifdef __cplusplus
    printf("This is C++\n" );
#else
    printf("This is C\n" );
#endif
    getchar();
    return 0;
}

ok Now that i got to allocate the string. Next step is to display the string in reverse order within the same function.

This article has been dead for over six months. Start a new discussion instead.