strcpy - cannot convert

This is a discussion on strcpy - cannot convert within the C Programming forums, part of the General Programming Boards category; Hi all, I'm trying to compile my program and I keep getting the error message Code: error C2664: 'strcpy' : ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    23

    strcpy - cannot convert

    Hi all,

    I'm trying to compile my program and I keep getting the error message
    Code:
    error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    What do I need to change to get around that ?

    Oh, and here is the little bit of code that is causing me my problem:
    Code:
    void time_hours_total(travel_record_type travels[])
    {
    	int position;
    	char temp_hours_time;
    	
    	if ((travels[position].date_of_arrival) == (travels[position].date_of_departure))
    	{
    		strcpy((travels[position].time_of_departure_hours) - (travels[position].time_of_arrival_hours), temp_hours_time);
    	}
    	else
    	{
    		strcpy((travels[position].time_of_departure_hours) +24 - (travels[position].time_of_arrival_hours), temp_hours_time);
    	}
    }
    Thanks in advance for any help

    H_M

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    First of all, you are compiling as C++, not C - not specifically the problem at hand but this is something to be avoided.

    Secondly, you haven't provided enough code to make a completely accurate diagnosis since we don't know what the struct travels (I'm assuming you're not actually coding in C++) contains.

    Now, down to the problems in the code you have: variables position and temp_hours_time are not initialised - that's a big no-no! That produces all sorts of undefined behaviour - so fix it!

    Secondly, look at the prototype of strcpy and what it does - it takes char*, (char pointers) - i.e. strings, and you are passing it a single character (position), and the other parameters look like integers - it's just not going to work.

    Lastly, since you are passing an array of structs to your function, you presumably want to loop through each element - look into that.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    I thought it was compiling as 'C' !? You'll have to forgive me on a couple of things, I am a beginner at this and just working my way through it.

    The struct 'travels' contains all the data that is entered in by the users e.g. name, address, destination, etc. and I am trying to count the travel time between the two destinations. These times are entered in by the user and I am trying to calculate it in a seperate prototype and then copy the output into a string too - is this a bad idea ? Would it help if I posted my code for that part too ?

    I don't understand by what you mean that they are not initialised ?

    Well, the data is held in strings and 'position' is where it is held in the database so it calculates the correct travel time and then stores it into a string. Am I going about this in completely the wrong way ? I thought that strcpy was the way to go ?

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Not initialized means you haven't given the variable a value. Also, please show us the struct you are using.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    Ah, I see, that makes sense to me now.

    The code of my struct is:
    Code:
    struct travel_record_type
    {
    	char member_name[MEMBER_NAME_SIZE+1];
    	char member_phone[MEMBER_PHONE_SIZE+1];
    	char member_address[MEMBER_ADDRESS_SIZE+1];
    	char vehicle_load[VEHICLE_LOAD_SIZE+1];
    	char date_of_arrival[DATE_OF_ARRIVAL_SIZE+1];
    	char time_of_arrival_hours[TIME_OF_ARRIVAL_HOURS_SIZE+1];
    	char time_of_arrival_mins[TIME_OF_ARRIVAL_MINS_SIZE+1];
    	char date_of_departure[DATE_OF_DEPARTURE_SIZE+1];
    	char time_of_departure_hours[TIME_OF_DEPARTURE_HOURS_SIZE+1];
    	char time_of_departure_mins[TIME_OF_DEPARTURE_MINS_SIZE+1];
    	char hours_time[HOURS_TIME_SIZE+1];
    	char total_time[TOTAL_TIME_SIZE+1];
    };
    Does this help?

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    right have a look the strcpy functiont prototype.
    Code:
    char *  strcpy ( char * dest, const char * src );
    it takes two char * as it paramter. but in your case u are passing a just a char which is different from a string or a char * (char []). so u got to send a string as a src and the destination is your struct member.

    As said before the temp_hours_time should be initiallized
    ssharish2005

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    Quote Originally Posted by ssharish2005
    right have a look the strcpy functiont prototype.
    Code:
    char *  strcpy ( char * dest, const char * src );
    it takes two char * as it paramter. but in your case u are passing a just a char which is different from a string or a char * (char []). so u got to send a string as a src and the destination is your struct member.

    As said before the temp_hours_time should be initiallized
    ssharish2005
    I know this will sound really stupid, but how would I do that ?? I thought I was passing the two of them as 'char' ?

    I am trying to work my way around it by using an old book that a friend passed to me, basically self learning and I'm absolutely puzzled on this one !

    You guys are geniuses on here, hopefully one day I'll be able to answer some questions on here. )

    H_M

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	char buffer[100]; //we wil copy something here
    	const char* ptr = "Hello, World!"; //this is a string we are going to copy
    	char array[]="Testing"; //this is another buffer its contents also can be copied
    	if(strlen(ptr) < sizeof(buffer) -1)
    	{
    	   strcpy(buffer, ptr);
    	   printf("This is a resulting buffer: \"%s\"\n",buffer);
    	}
    	if(strlen(array) < sizeof(buffer) -1)
    	{
    	   strcpy(buffer, array);
    	   printf("This is a resulting buffer: \"%s\"\n",buffer);
    	}
    
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    Thanks mate - that looks quite good! I'm going to try and work that into my code as mines differs slightly as I'm using 'cout' rather than 'printf' and I want to get the total into another string so it's saved.

    I'll need to try it later - that's my mum shouting on me telling me my dinner is ready.

    H_M

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    cout is not a C, it is C++
    If you are using C++ - use another forum and string class, there you don't need a strcpy

    If you are still with strcpy - take a look at strncpy
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    Thanks. I think my code might actually be a mixture of C and C++ although it started off as C!

    It'll need to be tomorrow I look at it now, have to go out now with my mum. Will let you know how I get on.

    Many thanks

    H_M

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    Vart,

    I tried your code example but still cannot get the 'strcpy' to work. (

    Using your code I can get it to work, but as soon as I start changing it to take either of my inputs, it fails miserably! I am determind to get this to work and I suppose you learn by examples and mistakes !

    How do I then get it to accept those two strings, calculate the difference and then copy that into another string ?

    Any help with this would be greatly appreciated.

    H_M

  13. #13
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    strcpy((travels[position].time_of_departure_hours) - (travels[position].time_of_arrival_hours), temp_hours_time);
    somehow I got the feeling that you want to assigne the differnece between arrival-time and departure-time to some other variable, and because all your variables are character arrays you are trying to use strcpy() to do that. If so, no that cannot be done.
    You have to convert the character arrays to ints before you can do any calculations on them. Try atoi().
    Kurt

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    Thanks Zuk - it can't be done with the way I'm doing it, that clears it up a bit more for me.

    I'm just having a look now for 'atoi()' and I'm going to try that, see how I get on.

    Many thanks ! Although, I will probably be back !

    H_M

  15. #15
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Please take note that atoi() is non-standard, and will not work with all compilers(Xcode for example).
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 08:42 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Replies: 3
    Last Post: 08-21-2006, 06:42 AM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 11:53 AM
  5. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 10:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21