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. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    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. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    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. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    You could use memcpy instead. Or a for-loop.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by Elysia View Post
    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. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    To make a copy of the data you can use memcpy() as noted before or strdup() as this deals with chars.

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Would help if you could explain what this program is trying to do.

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i think it sorts the words in some way
    Last edited by transgalactic2; 04-20-2009 at 11:01 AM.

  11. #11
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    these two functions are helping functions of another function
    and i cant understand the presented above "if"

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by transgalactic2 View Post
    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. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  3. array copying
    By kurz7 in forum C Programming
    Replies: 2
    Last Post: 05-07-2003, 01:43 AM
  4. Help to copy Array a into first portion of array b
    By Anna Lane in forum C Programming
    Replies: 4
    Last Post: 11-25-2002, 08:38 PM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21