By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

Swapping Two Strings.....

P: n/a
Hi Guys,
I have a small problem in the following code....I am using gcc compiler
in ubuntu .
I dont know why this code is giving error...
Can u any one please figure it out....

Code:

1 #include<stdio.h>
2 # define SIZE 50
3 void myswap(char**,char**);
4 int main()
5 {
6 char s1[SIZE]="C";
7 char s2[SIZE]="C++";
8 char *temp;
9 myswap(&s1,&s2);
10 printf("\nStr1=%s",s1);
11 printf("\nStr2=%s\n",s2);
12 }
13 void myswap(char **name1,char **name2)
14 {
15 char *temp;
16 temp=*name1;
17 *name1=*name2;
18 *name2=temp;
19 }

Error Msg:
swap.c: In function âmainâ:
swap.c:9: warning: passing argument 1 of âmyswapâ from incompatible
pointer type
swap.c:9: warning: passing argument 2 of âmyswapâ from incompatible
pointer type

But the following code is worrking....Check this code also...
1 #include<stdio.h>
2 #include<stdlib.h>
3 # define SIZE 50
4 void myswap(char**,char**);
5 int main()
6 {
7 char *s1;
8 char *s2;
9 s1=(char *)malloc(SIZE);
10 s2=(char *)malloc(SIZE);
11 printf("\nEnter a string : ");
12 fgets(s1,SIZE,stdin);
13 printf("\nEnter a string : ");
14 fgets(s2,SIZE,stdin);
15 myswap(&s1,&s2);
16 printf("\nStr1=%s",s1);
17 printf("\nStr2=%s\n",s2);
18 }
19 void myswap(char **name1,char **name2)
20 {
21 char *temp;
22 temp=*name1;
23 *name1=*name2;
24 *name2=temp;
25 }

Can anyone calrify my doubt?
My question is
1.Array name is pointer to base element....So i can pass the address of
array name(which is a pointer)....The code shud work as per the logic?
But its not so?

2.The same function myswap() is working for DMA variables.
Waiiting for proper reply?
Thanks in advance.....
MOHAN S

Apr 21 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Mohan wrote:
Hi Guys,
I have a small problem in the following code....I am using gcc compiler
in ubuntu .
I dont know why this code is giving error...
Can u any one please figure it out....

Code:

1 #include<stdio.h>
2 # define SIZE 50
3 void myswap(char**,char**);
4 int main()
5 {
6 char s1[SIZE]="C";
7 char s2[SIZE]="C++";
8 char *temp;
9 myswap(&s1,&s2);
10 printf("\nStr1=%s",s1);
11 printf("\nStr2=%s\n",s2);
12 }
13 void myswap(char **name1,char **name2)
14 {
15 char *temp;
16 temp=*name1;
17 *name1=*name2;


Hey! You can't do this, the compiler and linker/loader have worked hard
to locate s1 and s2 at particular addresses. They must remain at those
addresses for the duriation of execution of main(). You cannot change
their addresses without recompiling/linking. You need to actually copy
the string contents of s1 into a guaranteed large enough temp string,
then copy the contents of s2 into s1, ensuring of course that it will
fit, then copy the contents of the temp string into s2. Again, ensuring
that it will fit.
--
- Mark

Apr 21 '06 #2

P: n/a
Thanks mark....
I got calrified my doubt....

Apr 21 '06 #3

P: n/a
On 21 Apr 2006 05:08:21 -0700, "Mohan" <mo*********@gmail.com> wrote:
Hi Guys,
I have a small problem in the following code....I am using gcc compiler
in ubuntu .
I dont know why this code is giving error...
Can u any one please figure it out....

Code:

1 #include<stdio.h>
2 # define SIZE 50
3 void myswap(char**,char**);
4 int main()
5 {
6 char s1[SIZE]="C";
7 char s2[SIZE]="C++";
8 char *temp;
9 myswap(&s1,&s2);
s1 is an array of 50 char (char[50]). &s1 is a pointer to an array of
50 char (char (*)[50]. myswap requires an argument of type char**.
There is no implicit conversion between these to types. Hence the
syntax error messages.
10 printf("\nStr1=%s",s1);
11 printf("\nStr2=%s\n",s2);
12 }
13 void myswap(char **name1,char **name2)
14 {
15 char *temp;
16 temp=*name1;
*name1 is of type pointer to char (char*). This type usually has some
alignment requirements. You actually passed the address of an array
of char (char[50]). This type normally does not have any alignment
restrictions. If the array is not properly aligned so that the first
sizeof(char*) bytes can be treated as a char*, you have invoked
undefined behavior. Furthermore, on some systems evaluating an
invalid address (even without attempting to dereference it) can cause
undefined behavior.
17 *name1=*name2;
18 *name2=temp;
19 }

Error Msg:
swap.c: In function âmainâ:
swap.c:9: warning: passing argument 1 of âmyswapâ from incompatible
pointer type
swap.c:9: warning: passing argument 2 of âmyswapâ from incompatible
pointer type

But the following code is worrking....Check this code also...
1 #include<stdio.h>
2 #include<stdlib.h>
3 # define SIZE 50
4 void myswap(char**,char**);
5 int main()
6 {
7 char *s1;
8 char *s2;
9 s1=(char *)malloc(SIZE);
Don't cast the return from malloc. It doesn't help anything and can
suppress error messages you want to see.
10 s2=(char *)malloc(SIZE);
11 printf("\nEnter a string : ");
12 fgets(s1,SIZE,stdin);
13 printf("\nEnter a string : ");
14 fgets(s2,SIZE,stdin);
15 myswap(&s1,&s2);
16 printf("\nStr1=%s",s1);
17 printf("\nStr2=%s\n",s2);
18 }
19 void myswap(char **name1,char **name2)
20 {
21 char *temp;
22 temp=*name1;
23 *name1=*name2;
24 *name2=temp;
25 }

Can anyone calrify my doubt?
My question is
1.Array name is pointer to base element....So i can pass the address of
Not always. In particular, not when the operand of the & or sizeof
operators.
array name(which is a pointer)....The code shud work as per the logic?
The expression &arrayname evaluates to the address of the first
element of the array with type pointer to array type(type (*)[]). The
expression arrayname, except as noted above, evaluates to the address
of the first element with type pointer to element type (type*). These
types are not the same and there is no implicit conversion between
them.
But its not so?

2.The same function myswap() is working for DMA variables.


What is DMA? Which version of myswap?

If you always get the expected results when you execute code that does
not compile properly, you could probably live off lottery winnings or
the stock market because you have better luck than all the rest of us
combined.
Remove del for email
Apr 29 '06 #4

P: n/a
On 21 Apr 2006 05:39:37 -0700, "Mark Odell" <mr********@gmail.com>
wrote:
Mohan wrote: <snip attempt to treat &array as pointer to pointer>

An array is not a pointer, though it often evaluates to one. See
section 6 of the FAQ at usual places and http://c-faq.com .
Hey! You can't do this, the compiler and linker/loader have worked hard
to locate s1 and s2 at particular addresses. They must remain at those
addresses for the duriation of execution of main(). You cannot change
their addresses without recompiling/linking. You need to actually copy
the string contents of s1 into a guaranteed large enough temp string,
then copy the contents of s2 into s1, ensuring of course that it will
fit, then copy the contents of the temp string into s2. Again, ensuring
that it will fit.


Objects remain at fixed locations, yes. (Except sometimes dynamic
space you realloc, and that's actually a copy behind the scenes.)

For large (nonoverlapping) objects instead of a temp large enough for
the whole thing you can do it in chunks. At the extreme you can do it
one byte at a time with only one byte temporary, although this is <OT
platform dependent> likely to perform worse in most cases </>.

- David.Thompson1 at worldnet.att.net
May 1 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.