error c2664 'memcpy' : cannot convert parameter 1 from 'int' to 'void *'

Hi little new to c++, hope I am in the right section.

I researched the error code and its obviously to do with my data types, but because I am using enums I have gotten a little confused and not sure how to define the problem any further and or how to fix it.

When compilingi get the error below:

main.cpp(186) : error C2664: 'memcpy' : cannot convert parameter 1 from 'int' to
'void *'
Conversion from integral type to pointer type requires reinterpret_cast,
C-style cast or function-style cast

Any advice would be greatly appreciated, the line number wont be the same seen as a lot of commented out code I did not copy over.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <conio.h>
#include <malloc.h>

#define MAX_PROG_SIZE		20
#define MAX_PROG_OVERFLOW	40

#define Randint(UPPER) ((int)(random_number()*(UPPER)))

enum tokens {DUMMY, LEFT, RIGHT, MOVE, IF_FOOD_AHEAD, PROGN2, PROGN3, GHOST_NODE};

struct token_entry
{
   char *name;
   int arity_sub1;
   // function
};

struct token_entry token_table [] =
     { {"# ", -1},     // String end. Arity must be -1 for sub-string search
       {"LEFT ", -1},
       {"RIGHT ", -1},
       {"MOVE ", -1},
       {"IF_FOOD_AHEAD ", 1},
       {"PROGN2 ", 1},
       {"PROGN3 ", 2},
	   {"GHOST_NODE", -1}
     };

int main()
{
	int editdistance = 0;
	int i;
	int ar1 = 0; 
	int ar2 = 0;
	int a, b, j, x = 0;

	int s1[MAX_PROG_SIZE]={4, 5, 1, 6, 1, 2, 3, 3 };
	int s2[MAX_PROG_SIZE]={5, 1, 5, 2, 5, 4, 3, 1, 3};
	int c[MAX_PROG_OVERFLOW]={};
	int g[1]={7};
	
for ( i = 0; i < MAX_PROG_SIZE ; i++ )
{
	printf("s1 %d and s2 %d\n", s1[a],s2[b]);
		
  if (s1[a] != s2[b])
  {
	  printf("The edit distance is now %d\n", editdistance);
	  editdistance = editdistance + 1;
	  printf("The edit distance has increased to %d\n\n", editdistance);
  }
  else if (s1[a] == s2[b])
  {
	  printf("The edit distance is %d\n\n", editdistance);
  }
  else if (s1[a] == NULL)
  {
	  ar1 = 0;
  }
  else if (s1[b] == NULL)
  {
	  ar2 = 0;
  }
  
  printf("The edit distance is now %d\n", editdistance);

	switch( s1[a] )
		{
			case LEFT : // LEFT
				ar1 = 0;
				printf("I have got here, ar1 = %d\n", ar1);
					break;
			case RIGHT : // RIGHT
				ar1 = 0;
				printf("I have got here, ar1 = %d\n", ar1);
					break;
			case MOVE : // MOVE
				ar1 = 0;
				printf("I have got here, ar1 = %d\n", ar1);
					break;
			case IF_FOOD_AHEAD : // IF_FOOD_AHEAD
				ar1 = 2;
				printf("I have got here, ar1 = %d\n", ar1);
					break;
			case PROGN2 : // PROGN2
				ar1 = 2;
				printf("I have got here, ar1 = %d\n", ar1);
					break;
			case PROGN3 : // PROGN3
				ar1 = 3;
				printf("I have got here, ar1 = %d\n", ar1);
					break;
			case GHOST_NODE : // GHOST_NODE
				ar1 = 0;
				printf("I have got here, ar1 = %d\n", ar1);
					break;
			default :
				ar1 = 0;
				break;
			}
			switch( s2[b] )
			{
			case LEFT : // LEFT
				ar2 = 0;
				printf("I have got here, ar2 = %d\n", ar2);
					break;
			case RIGHT : // RIGHT
				ar2 = 0;
				printf("I have got here, ar2 = %d\n", ar2);
					break;
			case MOVE : // MOVE
				ar2 = 0;
				printf("I have got here, ar2 = %d\n", ar2);
					break;
			case IF_FOOD_AHEAD : // IF_FOOD_AHEAD
				ar2 = 2;
				printf("I have got here, ar2 = %d\n", ar2);
					break;
			case PROGN2 : // PROGN2
				ar2 = 2;
				printf("I have got here, ar2 = %d\n", ar2);
					break;
			case PROGN3 : // PROGN3
				ar2 = 3;
				printf("I have got here, ar2 = %d\n", ar2);
					break;
			case GHOST_NODE : // GHOST_NODE
				ar2 = 0;
				printf("I have got here, ar2 = %d\n", ar2);
					break;
			default :
				ar2 = 0;
				break;
			}
		
if ((ar1 - ar2) > (ar2 - ar1))
	{
	int k = 0;
		memcpy(c[k], s2[b+1], MAX_PROG_SIZE + 1); // This line is the one causing the problems
		printf("Memcpy complete s2\n");
	}
	
	a++;				
	b++;

	}
getch();
return 0;
}


As you can see above I am trying to cope the tail of array s2[b+1] to an empty array c[k], I am a little clueless any information would be greatly appreciated also any code improvements would be welcomed.

PS. If I am in the wrong thread please let me know where to post it so I can get a response.
Starting at line 40, are a and b, not yet initialized?

Line 40:
 
int a, b, j, x = 0; // only x is initialized 


Should be:
 
int a = 0, b = 0, j = 0, x = 0;
Last edited on
True, a and b are defined (that is memory has been allocated) but not initialised (assigned any specific value).

You can get the code to compile by doing this:
 
    memcpy((void *) &c[k], (void *) &s2[b+1], MAX_PROG_SIZE + 1);

but I've not verified whether this actually makes sense.

Your use of memcpy does not make sense.
What are you trying to do ? Something like c[k] = s2[b+1];?
@rcast sorry the variables were different before i was playing with them must of broken them but i fixed them again i was trying to optimise and it broke, should of left it the way it was.

int a = 0;
int b = 0;
int j = 0;
int x = 0;

Want to use it your way but didn't know how.

@ Chervil thank you that seems to have fixed the error on compile, i tried putting void * in but didn't know the correct syntax or the reason for it. Does the & symbol refer to what you are using?

@Toum something like that but a bit more in-depth i am creating a function for genetic programming.

Thank you for all your help.
Last edited on
The & symbol gives the address of the object. In this case, the object is an element of the array, so it gives the address part-way into the array.
Ahh excellent and the void * just means that there is no specific data type? or am i wrong?

Really appreciate the help no idea why i didn't post sooner been stuck on this for days.
without the (void *), the & gives a pointer to an int. It needs to be cast to a void type (non-specific) as that is what is expected by function memcpy().

http://www.cplusplus.com/reference/cstring/memcpy/
Thank you i understand it now, Ii read over that page a few times just didn't sink in. Plus the way you wrote the line is slightly different still I have to much to learn, thank you very much.
Topic archived. No new replies allowed.