Creating a function that swaps char array using pointers

For an assignment I have to make two functions that swap character arrays.

One using void swap( char str1[], char str2[] ); and the other using void swap( char *str1, char *str2);

I have the first function working correctly, but I don't know how you are supposed to swap using pointers. Won't you need a pointer for every address in the array?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  #include <stdio.h>
void swap(char str1[5], char str2[5]);
void swapper( char* str1, char* str2);
int main()
{
    int i =0;
    char firstname[5] = {'A', 'L', 'A', 'N'}, lastname[5] = {'D'};
    swap(firstname, lastname);
    swap(&firstname, &lastname);
    for(; i<5; i++)
    {
        if(firstname[i]!='\0')
        printf("%c\n", firstname[i]);
    }
    return 0; 
}
void swap( char str1[5], char str2[5])
{
    int j=0;
    char temp[5];
    for(;j<5; j++)
    {
        temp[j]=str1[j];
        str1[j]=str2[j];
        str2[j]=temp[j];
    }
}
void swapper( char* str1, char* str2);
{
    char *temp = *str1;
    *str1 = *str2;
    *str2=temp;
}
closed account (SECMoG1T)
The function will be similar to the one on line 17 , you arrays are implicitly converted to pointers , just add ab offset to get your current pointer such that your line 24 is equivalent to

*(str1+j)=*(str2+j);//pointer arithmetics
I don't know what ab offset is. I tried writing it like this but it still keeps saying

"operand of indirection operator * is not a pointer"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
void swap(char str1[5], char str2[5]);
void swapper( char* str1, char* str2);
int main()
{
    int i =0;
    char firstname[5] = {'A', 'L', 'A', 'N'}, lastname[5] = {'D'};
    swap(firstname, lastname);
    swap(&firstname, &lastname);
    for(; i<5; i++)
    {
        if(firstname[i]!='\0')
        printf("%c\n", firstname[i]);
    }
    return 0; 
}
void swap( char str1[5], char str2[5])
{
    int j=0;
    char temp[5];
    for(;j<5; j++)
    {
        temp[j]=str1[j];
        str1[j]=str2[j];
        str2[j]=temp[j];
    }
}
void swapper( char* str1, char* str2);
{
    int k=0;
    for (;k<5; k++)
    {
    char *temp;
    *(temp+k) = *(str1+k);
    *(str1+k) = *(str2+k);
    *(str2+k)=(temp+k);
    }
}
closed account (SECMoG1T)
Ooh sorry I din't mean ab offset I meant an offset, typo.

Line 9 , that's not the way to call the function because
your arrays are implicitly converted to pointers
so you should pass them directly to the function lyk this swap(firstname, lastname);

1
2
*(temp+k) = *(str1+k);/// line 34 replace with temp=*(str1+k);
*(str2+k)=(temp+k);/// line 36 replace with *(str2+k)=temp; 
Last edited on
Thank you for all the help so far.

I am still getting an error for lines 34, 35 and 36. Maybe I have to use **?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
void swap(char str1[5], char str2[5]);
void swapper( char* str1, char* str2);
int main()
{
    int i =0;
    char firstname[5] = {'A', 'L', 'A', 'N'}, lastname[5] = {'D'};
    swap(firstname, lastname);
    swapper(firstname, lastname);
    for(; i<5; i++)
    {
        if(firstname[i]!='\0')
        printf("%c\n", firstname[i]);
    }
    return 0; 
}
void swap( char str1[5], char str2[5])
{
    int j=0;
    char temp[5];
    for(;j<5; j++)
    {
        temp[j]=str1[j];
        str1[j]=str2[j];
        str2[j]=temp[j];
    }
}
void swapper( char* str1, char* str2);
{
    int k=0;
    for (;k<5; k++)
    {
    char *temp;
    temp = *(str1+k);
    *(str1+k) = *(str2+k);
    *(str2+k)=temp;
    }
}
closed account (SECMoG1T)
No that's not the error

line 33
char *temp; ///should be... char temp
Topic archived. No new replies allowed.