Thread: Convert char* to char?

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    15

    Question Convert char* to char?

    I would like to strcpy() the content of the memoryblock pointed on by my_pointer to my_variable.
    Code:
    strcpy (my_variable, my_pointer);
    Of course it does not work. How can I solve it?
    Thank You.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    my_variable should have enough memory allocated to it.

    IOW:
    Code:
    char my_variable[MAX_ARRAY];
    ..
    ..
    ..
    strcpy(my_variable, my_pointer);
    Of course, my_pointer should be pointing to a string in this case.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    Unfortunately the compiler tells me that now parameter 2 cannot be changed from 'char' to 'const char*'...

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    You'll need to post more code.

    Preferably the declaration of my_pointer and my_variable and where you allocate memory for my_pointer.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    my_pointer is probably pointing to a single char rather than a string.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    (ok, it's my first script in c++ so it became a little dirty... and long... and over-nested...)

    I employ strtok() in one part of the programm and get char* var_Desk. Then I want to check, what var_Desk contains and return either var_Desk as var_Deskcheck or "0" as var_Deskcheck.
    Code:
    char fnc_Interpret (char lkv_Desk)
    {
    	char var_Deskcheck [8];
    
    	if (lkv_Desk != 0) strcpy (var_Deskcheck, lkv_Desk);
    	else if (lkv_Desk == 0) strcpy (var_Deskcheck, "0");
    
    	return var_Deskcheck;
    }
    (I my code "my_variable" is "var_Deskcheck" and "my_pointer" is "var_Desk".)

    Thanks.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    @ elad:
    I thought I pointed to whatever strtok() returns, but I found no explicit explanation what strtok() really does...

  8. #8
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Lots of problems here:

    - It's complaining because lkv_Desk is type char. You're passing it into strcpy which takes type char*

    - And what's the purpose of this comparison?
    (lkv_Desk != 0)
    If you're checking for the NULL case then your else statement can simply be
    var_Deskcheck[0] = '\0';

    - return var_Deskcheck;
    You can't do this because your char array will be out of scope after you've returned it. Either make a global char array or better yet, do a malloc and return the pointer.

    Is this what you want?
    Code:
    char* fnc_Interpret (char* lkv_Desk)
    {
       char* var_Deskcheck;
       var_Deskcheck = malloc(sizeof(char)*8);
    
       if (lkv_Desk != NULL)
          strcpy (var_Deskcheck, lkv_Desk);
       else
          var_Deskcheck = NULL;
       return var_Deskcheck;
    }
    Last edited by Cshot; 10-03-2002 at 01:43 PM.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. convert long to pointer to char array
    By gazmack in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2003, 11:33 AM
  5. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 11:11 AM