Hi every one,

include <stdio.h>
include <conio.h>
define SIZE 4

int
main(){
char Menu[SIZE] = {'A','B','C','D'};
int i;

for(i=0; i<(SIZE); i++){            // print the array elements
    printf("\t%c",Menu[i]);
}   
getch();

}

I want to swap the elements of the array for e.g. if I pass A and D or Menu[0], Menu[3], then the resulting array should be something like this when I print it:
D, B, C, A

I want to make use of pointers, I am new to C so don't know how to go about it......

Regards.

Recommended Answers

All 4 Replies

I want to make use of pointers, I am new to C so don't know how to go about it

Have you read about pointers? If not, get a good book, read about it. The forum lists a lot of good books on C. In case you still have doubts after reading it, you can post here.

Swap elements 1 and 3:

void swap1and3(void)
{
    char* p1 = &menu[1];
    char* p2 = &menu[3];
    char tmp;

    tmp = *p1;
    *p1 = *p2;
    *p2 = tmp;
}

So, a more generic offering:

void swapChars( char* p1, char* p2)
{
    char tmp;
    tmp = *p1;
    *p1 = *p2;
    *p2 = tmp;
}

Thanks a lot rubberman :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.