Skip to main content
gamedev.net
🔒 Locked

Passing data between variables

Started by lacsap Oct 14, 2001 at 1:37 PM 5 replies 400+ views
Original Post
lacsap
lacsap
I have a problem. I''m writing a program that allows you to sort 10 names that you''ve entered, and as a sorting algorythm I use bubblesort. But now I also need to swap data from 1 position to another, but I keep getting all these errors. Could someone help me ? it is as following: char n[10][255] is a global variable to store the names in. in the swap function, I have the following: char *tmp_n = &n[n1]; n[n1] = n[n2]; n[n2] = tmp_n; tmp_n is the temporary var. n1 and n2 are the numbers of wich items are about to be swapped: For example: If you want to swap n[5] and n[6], the syntax for the swap_items function is as following: swap_items(5,6). When I compile I get the following output: (I use MSVC++ 6.0) --------Configuration: SortTest - Win32 Debug-------------------- Compiling... main.cpp E:\School\SortTest\main.cpp(49) : error C2440: ''initializing'' : cannot convert from ''char (*)[255]'' to ''char *'' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast E:\School\SortTest\main.cpp(51) : error C2106: ''='' : left operand must be l-value E:\School\SortTest\main.cpp(52) : error C2440: ''='' : cannot convert from ''char *'' to ''char [255]'' There are no conversions to array types, although there are conversions to references or pointers to arrays Error executing cl.exe. SortTest.exe - 3 error(s), 0 warning(s) CAN SOMEONE HELP ME WITH THIS ????? pascal
DDnewbie
DDnewbie
try this:

char *tmp_n = n[n1];

That is, no &-operator.


/Mankind gave birth to God.
/Mankind gave birth to God.
Oluseyi
Oluseyi
quote:
Original post by lacsap
E:\School\SortTest\main.cpp(49) : error C2440: ''initializing'' : cannot convert from ''char (*)[255]'' to ''char *''
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


The compiler suggests type casting:

char *tmp_n = (char *)n[n1];

(char *)n[n1] = (char *)n[n2];
(char *)n[n2] = tmp_n;


The type casts force the compiler to evaluate the expressions ''n[n1]'' and ''n[n2]'' as pointers to regions of memory rather than fixed length arays. This works because n[0] is a memory location (the ''0'' is also the offset from n).
lacsap
lacsap
I''ve used :

char *tmp_n = (char *)n[n1];
(char *)n[n1] = (char *)n[n2];
(char *)n[n2] = tmp_n;

but now I''ll get the compiler errors:

--------Configuration: SortTest - Win32 Debug--------------------
Compiling...
main.cpp
E:\School\SortTest\main.cpp(57) : error C2106: ''='' : left operand must be l-value
E:\School\SortTest\main.cpp(58) : error C2106: ''='' : left operand must be l-value
Error executing cl.exe.

SortTest.exe - 2 error(s), 0 warning(s)
lacsap
lacsap
(I forgot: line 57 and 58 are the last two lines of code in the above( (char *)n[nr1] = (char *)n[nr2]; and (char *)n[nr2] = tmp_n; )
Dactylos
Dactylos
Since you declare the name array with a fixed length (statically allocated), you can''t change the ''pointers'' in the array (because statically allocated 2-dimensional arrays aren''t implemented with pointers in C, only dynamically allocated arrays are).

Declare n as an array of char pointers and allocate the memory for the name-strings dynamically. Then do as silvren suggested.

e.g. you can do it like this:
  
// declare the name-array

const int NUMBER_OF_NAMES = 10;
char * n[NUMBER_OF_NAMES];

// allocate memory for the 10 names

for (int i = 0; i < NUMBER_OF_NAMES; ++i)
n[i] = new char[256];

// use the names here...

// to swap two names with index n1 and n2 do:

char *tmp_n = n[n1];
n[n1] = n[n2];
n[n2] = tmp_n;

// deallocate the names

for (int i = 0; i < NUMBER_OF_NAMES; ++i)
delete [] n[i];

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.