Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning Arrays to Variables (and vise versa)

Status
Not open for further replies.

Sklar

Technical User
Oct 24, 2005
2
US
Is there a way to set a variable to equal an array
(ie. temp = name[index]) and then be able to set another array equal to that variable(ie. name[index + 1] = temp).

Danka
Sklar
 
Yup. Exactly the way you just wrote it. Except when you say "array" and "another array," you really mean "element of an array" and "another element of an array (in this case, of the same array)."
 
Hello again,

I tried using the [index + 1] = temp) statement, but I get some: '=' : cannot convert from 'char *' to 'char [25]' compile time error. Here is (part of the) function (trying to sort words in a file): Any suggestions why I get this error.

void sort(char word[][25], int size, int& number)
{
using namespace std;
int index, result;

ifstream fin;
fin.open("bob.txt");

for(index = 0;! fin.eof() && index < 100; index++) //Fills Array
{
fin.getline(word[index], 25);
}
number = index;

for (index = 0; index < number; index ++)
{
result = strcmp(word[index], word[index +1]);

if( result > 0 ) //Sort words
{
char *temp;
temp = word[index];
strcpy (word[index], word[index +1]);
word[index + 1] = temp;
}

}
}

The program works if I remove word[index + 1] = temp, but I need that line to switch the words.

Thanks for Your Help
Sklar
 
you have to do the full string copy to swap your two strings.
char temp[25];
strcpy(temp,word[index]);
strcpy (word[index], word[index +1]);
strcpy(word[index + 1], temp);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top