473,326 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Swapping Two Strings.....

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
4 6926
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
Thanks mark....
I got calrified my doubt....

Apr 21 '06 #3
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Christopher Jeris | last post by:
Please help me understand the differences, in semantics, browser support and moral preferredness, between the following three methods of swapping content in and out of a page via JavaScript. I...
16
by: Sethu Madhavan | last post by:
Hi All Here i am facing one technical issue related to Swapping the Object Instance in Dotnet. e.g.. class Customer{ Order _lastOrder;
4
by: alanrn | last post by:
I am using a TreeView to display the hierarchy of a strongly-typed collection (inherited from CollectionBase). The order of the nodes in the TreeView is strictly tied to the order in which they...
270
by: Jatinder | last post by:
I found these questions on a web site and wish to share with all of u out there,Can SomeOne Solve these Porgramming puzzles. Programming Puzzles Some companies certainly ask for these...
7
by: anonymous | last post by:
Hi CLCers, I tried the following code for swapping a string, but it is not working. Inside the swap function the strings are printed correctly, but when back in main() the strings are not swapped...
28
by: rajendra.stalekar | last post by:
Hi Folks!!! I have a string let's say "hi" and got to reverse it using just a single variable for swapping, how do I do it? Regards, Rajendra S.
2
by: Sam Marrocco | last post by:
Does anyone know of a method of swapping collection items? For example: Current collection: ..Item(1)="a" ..Item(2)="b" ..Item(3)="c" After swapping items 2 and 1.... ..Item(1)="b"...
9
by: storyGerald | last post by:
I knew some ways of swapping two ints without using a temporary variable. Just like: // Method 1: void swap1(int &a, int &b) { int temp = a; a = b; b = a; }
2
by: Protoman | last post by:
How would I write a user defnable letter swapping algorithm? I've written an Enigma encrypting program, and I need to redo the plugboard function. Right now, the letter swapping is fixed. I need to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.