-
June 20th, 2013, 04:31 PM
#1
Code does not work!!!
Hi everybody!!!!
Could you tell me why the following code doesn't work???
That is the code:
Code:
#include <stdio.h>
#include <string.h>
void func(char *p1,char *p2){
char ch;
while (p1<p2){
ch=*p1;
*p1=*p2;
*p2=ch;
p1++;
p1--;
}
}
int main()
{
char a[8]="VENICE";
puts(a);
func(a,a+4);
puts(a);
return 0;
}
Thanks in advance!
-
June 20th, 2013, 04:49 PM
#2
Re: Code does not work!!!
You are trying to modify a string literal ("VENICE"). This will cause undefined behaviour.
-
June 21st, 2013, 03:04 PM
#3
Re: Code does not work!!!
Originally Posted by Peter_B
You are trying to modify a string literal ("VENICE"). This will cause undefined behaviour.
Isn't he trying to modify the contents of memory whose start address is the start address of the char array a whose contents have been set to VENICE\0? Surely changing p1-- to p2-- is all that is required in this case? This works OK with my c compiler and produces the expected output.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 22nd, 2013, 02:06 PM
#4
Re: Code does not work!!!
Originally Posted by 2kaud
Isn't he trying to modify the contents of memory whose start address is the start address of the char array a whose contents have been set to VENICE\0? Surely changing p1-- to p2-- is all that is required in this case? This works OK with my c compiler and produces the expected output.
Yes, I got it wrong with the string literal bit mathmari's code wasn't trying to alter the literal itself, but rather the copy which is in the char array. Thanks for correcting me. I'm so used to seeing beginners trying to modify string literals that I jumped to the wrong conclusion.
It is indeed just the p1-- which was wrong, and should be p2--.
-
June 20th, 2013, 04:51 PM
#5
Re: Code does not work!!!
So....how can I change my code so that it works???
-
June 20th, 2013, 05:00 PM
#6
Re: Code does not work!!!
Either:
1) Change to use std::string instead of char arrays, or
2) Create a char array on the heap using new and initialize this array to contain "VENICE" (plus the null-terminator)
-
June 20th, 2013, 05:03 PM
#7
Re: Code does not work!!!
You mean like that:
Code:
#include <stdio.h>
#include <string.h>
void func(char *p1,char *p2){
char ch;
while (p1<p2){
ch=*p1;
*p1=*p2;
*p2=ch;
p1++;
p1--;
}
}
int main()
{
char a[8];
printf("Give a string:\n");
gets(a);
puts(a);
func(a,a+4);
puts(a);
return 0;
}
????The output is still wrong
-
June 20th, 2013, 05:10 PM
#8
Re: Code does not work!!!
You have a little bug in func(). Run it through the debugger -you'll spot it eventually...
Also, don't use gets() - it's dangerous, and has been deprecated. For input it is safer to use streams (i.e. cin or getline)
Last edited by Peter_B; June 20th, 2013 at 05:14 PM.
-
June 20th, 2013, 05:20 PM
#9
Re: Code does not work!!!
I haven't got it and also I haven't got taught streams.Could you explain me again???
-
June 21st, 2013, 07:27 AM
#10
Re: Code does not work!!!
Originally Posted by mathmari
I haven't got it and also I haven't got taught streams.Could you explain me again???
Streams are used in c++ - not in c which I understand from your previous posts you are using. Instead of using gets, it's better to use fgets for which you specify the maxmimum number of chars to get and also null terminates the got string.
Replace
with
Code:
fgets(a, 8, stdin);
In your func()
Are you sure you want to increment p1 and then decrement it?????
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 21st, 2013, 09:01 AM
#11
Re: Code does not work!!!
I changed the code:
Code:
#include <stdio.h>
#include <string.h>
void func(char *p1,char *p2){
char ch;
while (p1<p2){
ch=*p1;
*p1=*p2;
*p2=ch;
p1++;
p2--;
}
}
int main()
{
char a[8];
printf("Give a string:\n");
gets(a);
puts(a);
func(a,a+4);
puts(a);
return 0;
}
and now the output is right....Thank you very much!!!!!!!!!!
-
June 21st, 2013, 08:59 AM
#12
Re: Code does not work!!!
Ok...nice...Oh no,I am sorry I wanted to decrement p2!!!!
-
June 21st, 2013, 09:54 AM
#13
Re: Code does not work!!!
But your code still has some problems. You are still using gets() rather than fgets(). Also with func(a, a + 4), what if the user enters a string with less than 5 characters?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 21st, 2013, 11:41 AM
#14
Re: Code does not work!!!
I want that my code works,for the string VENICE...If I use fgets,do I not have to create a file???
Originally Posted by 2kaud
But your code still has some problems. You are still using gets() rather than fgets(). Also with func(a, a + 4), what if the user enters a string with less than 5 characters?
-
June 21st, 2013, 10:21 AM
#15
Re: Code does not work!!!
Originally Posted by 2kaud
Streams are used in c++ - not in c
Technically, C has the notion of input/output streams too, but they would not be the C++ I/O streams that Peter_B mentioned, but rather say, stdin and in this case the use of fgets.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|