# making a copy of an array by copying its address..

This is a discussion on making a copy of an array by copying its address.. within the C Programming forums, part of the General Programming Boards category; cant see why it works ptemp=word[i]; i remember a thread of some user that asked of ways to do a ...

1. ## making a copy of an array by copying its address..

cant see why it works
ptemp=word[i];

i remember a thread of some user
that asked of ways to do a copy of an array.
and i suggested him to do this
thing.
and then i have been criticized by that "solution"
that we cant make a copy of an array just by assigning its address to other pointer
because (it cannot be copied or something like that)
Code:
```int maxNum(char *str) {
int i, j, max=0, count;
for(i=0; str[i]; i++){
count=1;
for(j=i+1; str[j]; j++)
if(str[i]==str[j])
count++;
if(count >max)
max = count;
}
return max;
}
void sort(char** word, int size){
int i, j;
char *ptemp;
for(i=0; i<size-1; i++)
for(j=i+1; j<size; j++){
if(maxNum(word[i])<maxNum(word[j])||
(maxNum(word[i])==maxNum(word[j]) && strcmp(word[i], word[j])>0)){
ptemp=word[i];
word[i]=word[j];
word[j]=ptemp;
}
}
}```

2. IMO be clear in your requirements else it's very hard to make sense of what you're saying.
So with that said what's the need of copying an array??

3. You can't copy an array by copying its address, because it does exactly what you say it does - copy its address.
So now you have two pointers pointing to the SAME data source.
That's why it's bad.

4. You could use memcpy instead. Or a for-loop.

5. cant see why it works
ptemp=word[i];
This works before you are reordering pointers within an array of pointers. However, in your examples no copies are made of any array.

6. Originally Posted by Elysia
You can't copy an array by copying its address, because it does exactly what you say it does - copy its address.
So now you have two pointers pointing to the SAME data source.
That's why it's bad.
so i ll have two pointers for the same array

thanks

7. To make a copy of the data you can use memcpy() as noted before or strdup() as this deals with chars.

8. maxnum finds the char which appears the most in a single word
i cant understand why they do this if in the sort function.
Code:
```if(maxNum(word[i])<maxNum(word[j])||
(maxNum(word[i])==maxNum(word[j]) && strcmp(word[i], word[j])>0))```

9. Would help if you could explain what this program is trying to do.

10. i think it sorts the words in some way

11. these two functions are helping functions of another function
and i cant understand the presented above "if"

12. Originally Posted by transgalactic2
maxnum finds the char which appears the most in a single word
i cant understand why they do this if in the sort function.
Code:
```if(maxNum(word[i])<maxNum(word[j])||
(maxNum(word[i])==maxNum(word[j]) && strcmp(word[i], word[j])>0))```
perhaps because it's sorting in descending order!

13. That would mean that it uses maxNum as the first sorting criterion and if these are equal falls back to regular string comparison (descending order) as the second criterion.

Calling a n ^ 2 complexity algorithm potentially twice hurts the eyes a bit though, and the indentation could be better, for example a trap like this:

Code:
```		for(j=i+1; str[j]; j++)
if(str[i]==str[j])
count++;
if(count >max)
max = count;```

Popular pages Recent additions