passing the address of an array of pointers? 
Author Message
 passing the address of an array of pointers?

I'm trying to send the address of a pointer in an array of pointers of char.

the call is:

swap(&ptrarr[length],&ptrarr[length/2]));

the function header is:

void swap(char *ptr1,char *ptr2)

This works perfectly fine. However, I get warnings out the butt. Is there any
 cleaner way to do this that doesn't irritate the compiler?



Sun, 09 Apr 2000 03:00:00 GMT  
 passing the address of an array of pointers?



Quote:
> I'm trying to send the address of a pointer in an array of pointers of
char.

> the call is:

> swap(&ptrarr[length],&ptrarr[length/2]));

> the function header is:

> void swap(char *ptr1,char *ptr2)

> This works perfectly fine. However, I get warnings out the butt. Is there
any
>  cleaner way to do this that doesn't irritate the compiler?

So there is
char *ptrarr[N]; /* An array of pointers to char */

This means that ptrarr[length] is a pointer to a char.
And &ptrarr[length] is a pointer to a pointer to a char.

So there's to solutions (depending on the implementation of swap()):
Either declare
        void swap(char **ptr1,char **ptr2)
Or call
        swap(ptrarr[length],ptrarr[length/2]);

The first allows you to swap the pointers in the array. The
second only allows you to swap the content of the characters
that is pointed to.

Marco.



Sun, 09 Apr 2000 03:00:00 GMT  
 passing the address of an array of pointers?

Quote:

> I'm trying to send the address of a pointer in an array of pointers of char.

> the call is:

> swap(&ptrarr[length],&ptrarr[length/2]));

> the function header is:

> void swap(char *ptr1,char *ptr2)

> This works perfectly fine. However, I get warnings out the butt. Is there any
>  cleaner way to do this that doesn't irritate the compiler?

Hi RMill1e1,

I think you really want to take a pointer from an array of pointers to
char into
a pointer and not a pointer into an array of pointers of char (oououu
very hard):)

My Codeexample runs without any warnings -
I dont know what you really want without to see the other code from
you.

Look at my Code an mail me back if im right or wrong-
perhaps the other guys have an another example.

regards,

Otmar

------code ---- tested with gnu lcc-win32 and Borland C++ 4.5

#include <stdio.h>
#include <stdlib.h>

char *arr[4];

void swap(char *ptr1, char *ptr2)
{
        char *temp = (char*) malloc(6);
        while(*ptr1)
        {
                *temp = *ptr1;
                *ptr1 = *ptr2;
                *ptr2 = *temp;
                temp++;
                ptr1++;
                ptr2++;
        }

Quote:
}

int main(void)
{
        arr[0] = "AAAAA";
        arr[1] = "BBBBB";
        arr[2] = "CCCCC";
        arr[3] = "DDDDD";

        printf("arr[0] = %s \n", arr[0]);
        printf("arr[1] = %s \n", arr[1]);
        printf("arr[2] = %s \n", arr[2]);
        printf("arr[3] = %s \n", arr[3]);

        swap(arr[0], arr[1]);
        swap(arr[2], arr[3]);

        printf("arr[0] = %s \n", arr[0]);
        printf("arr[1] = %s \n", arr[1]);
        printf("arr[2] = %s \n", arr[2]);
        printf("arr[3] = %s \n", arr[3]);

        return 0;

Quote:
}

--
****Otmar Schwan  TyBrain Consulting  Heidenheim/Germany ***
*** Web:    www.t-online.de/home/otmar.schwan **********

************************************************************


Sun, 09 Apr 2000 03:00:00 GMT  
 passing the address of an array of pointers?

Quote:

>I'm trying to send the address of a pointer in an array of pointers of
char.

>the call is:

>swap(&ptrarr[length],&ptrarr[length/2]));

>the function header is:

>void swap(char *ptr1,char *ptr2)

>This works perfectly fine. However, I get warnings out the butt. Is there
any
> cleaner way to do this that doesn't irritate the compiler?

Hmmm... it would have helped if you had included the declaration of
'ptrarr'.  However, I'm going to guess that 'ptrarr' is truly an array of
"char *" and that your swap() function is intended to swap elements of this
array.  If this is true, just define swap() as follows:

    void swap(char** ptr1, char** ptr2)
    {
        char* temp = *ptr1;    /* save "char*" from location 1 */
        *ptr1 = *ptr2;    /* copy "char*" from location 1 to location 2 */
        *ptr2 = temp;    /* copy original "char*" from location 1 into
location 2 */
    }

In other words, swap() accepts pointers to two "char *" values and exchanges
the "char *" values in these two locations.

-- Tom

/==================================================================\
 Thomas W. Brown                         Director of Development

 Phone: (619)676-7854               10875 Rancho Bernardo Rd., #200
 Fax  : (619)676-7710                      San Diego, CA  92127
\==================================================================/



Tue, 11 Apr 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Passing pointers to arrays of pointers....

2. passing address of a pointer as argument to a function

3. struggling all day long: passing by address array of structures

4. pointers and pass by address or value?

5. Pass address inside a pointer to a function

6. passing address pointers

7. Passing address of an array of structs

8. Passing an array of addresses...

9. How to pass array address?

10. assign address to pointer array

11. Pointers & incrementing array address

12. Long as array/pointer address

 

 
Powered by phpBB® Forum Software