Passing data between variables

Started by
5 comments, last by lacsap 22 years, 6 months ago
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
Advertisement
try this:

char *tmp_n = n[n1];

That is, no &-operator.


/Mankind gave birth to God.
/Mankind gave birth to God.
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).
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)
(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; )
I think you must copy whole strings from one place to another but not pointers. Try this:

  char n[10][255];  void swap_items(int n1, int n2){  char tmp_n[255];  strcpy(tmp_n, n[n1]);  strcpy(n[n1], n[n2]);  strcpy(n[n2], tmp_n);}  
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-arrayconst int NUMBER_OF_NAMES = 10;char * n[NUMBER_OF_NAMES]; // allocate memory for the 10 namesfor (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 namesfor (int i = 0; i < NUMBER_OF_NAMES; ++i)   delete [] n[i];  

This topic is closed to new replies.

Advertisement