The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
An Average programmer gone Novice trying to be Average again.
Discuss An Average programmer gone Novice trying to be Average again. in the C Programming forum on Dev Shed. An Average programmer gone Novice trying to be Average again. C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 24th, 2003, 07:59 PM
|
|
Junior Member
|
|
Join Date: Mar 2003
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
An Average programmer gone Novice trying to be Average again.
I took C++ programming a very long time ago, but fell out of practice. I picked up PHP and blew it away, but now, bringing myself back to C++ is a route full of treachery and murderous heathens who want my scalp!
Anyways, I'm a bit shakey overall, so some help would be most appreciative.
I run...
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(int argc, char* argv[])
{
char* arg;
scanf("%s", arg);
printf("%s\n", arg);
getch();
return 0;
}
And I get...
0xC0000005: Access Violation
I hate my life.
|

March 24th, 2003, 11:16 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
I followed an eerily similar path, and now I'm re-learning C++, but I don't recognize any of your code. I looked up scanf in a book I have in my closet, and they put a dimension on the char*, which seems strange to me:
Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(int argc, char* argv[])
{
char* arg[20];
scanf("%s", arg);
printf("%s\n", arg);
getch();
return 0;
}
I would code that like this:
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string text;
cout<<"Enter some text:\n";
cin>>text;
cout<<"You entered: "<<text<<endl;
return 0;
}
Last edited by 7stud : March 24th, 2003 at 11:24 PM.
|

March 24th, 2003, 11:21 PM
|
|
Junior Member
|
|
Join Date: Mar 2003
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
I guess I gotta shy away from those on-the-fly declared scalars in PHP, huh :-D
from what I looked up, scanf enjoys strings, not chars.
thanks
|

March 25th, 2003, 12:52 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
The error is because char *arg is not pointing to anything (i.e.) it is uninitialized. So when scanf reads the string and tries to assign it to arg, it ends up writing to wherever *arg is pointing to, which is causing it to crash. To fix this, make *arg point to something using malloc() or calloc(), like this:
Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <malloc.h>
int main(int argc, char* argv[])
{
char* arg;
/* Allocate 100 character array */
arg = (char *) malloc(100 * sizeof(char));
scanf("%s", arg);
printf("%s\n", arg);
getch();
free(arg); /* Don't forget to delete what we allocated */
return 0;
}
Alternatively, you could declare arg as a char array instead of a char pointer and not use malloc(), like this:
Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(int argc, char* argv[])
{
char arg[100];
scanf("%s", arg);
printf("%s\n", arg);
getch();
return 0;
}
Either way should work well. Hope this helps 
|

March 25th, 2003, 03:42 AM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
Scorpions4ever,
Then can I assume in C, this creates an array of pointers:
char* arg[20];
However, each pointer would seemingly be unitialized, so it still shouldn't work, but the code does work.  (C doesn't make sense. Good thing Bjarne created some order out of that chaos.)
Last edited by 7stud : March 25th, 2003 at 03:45 AM.
|

March 25th, 2003, 07:44 AM
|
|
|
|
It doesn't work. It only appears to work. When arg is a single pointer to char its value is used as the parameter to scanf. Since it is uninitialized you get an access violation when it is dereferenced. When arg is an array of pointers to char its address is passed to scanf. Since this is a valid address scanf will stuff characters into memory at that address. In effect, you have created an array of 80 bytes and that is how scanf uses it. If you look at the memory in a debugger, you will see that the characters you scanned in are packed into the elements of arg.
|

March 25th, 2003, 06:22 PM
|
|
Junior Member
|
|
Join Date: Mar 2003
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
I understand the pointer argument and it makes sense.
As I try char arg[25], the code works fine.
However, say I wish to use strlen or strcmp... both of them require a const char *. How would I go about using them in the event that I using the forementioned syntax array?
Also, as I review a great dealof SMAUG mud code, all of the strings are char* string. It is very strange to me.
|

March 30th, 2003, 03:50 AM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
"How would I go about using them in the event that I using the forementioned syntax array?"
In many situations, you can use an array name as if it were a pointer. To see that, print out a pointer variable--the result will be an address. If you print out an array name, the result will also be an address. The most significant difference between an array name and a pointer is that you can modify the address stored in a pointer, while the address that an array name refers to is fixed.
"I wish to use strlen or strcmp... both of them require a const char *. "
I don't know if you know this or not, but you don't have to send a constant pointer to a function that has a const pointer parameter. The way I look at is that a function which has a const designation in front of a parameter will take any parameter of the designated type const or non constant. What the const designation does is advertise that the function won't change the parameter. That allows the user/programmer to send a pointer to the function without worrying that the function will change the value pointed to by the pointer.
You can run this code as a test:
Code:
#include <iostream>
using namespace std;
void print(const char* text)
{
cout<<text<<endl;
}
int main()
{
const char* texta ="Hello.";
char* textb = "Goodbye.";
print(texta);
print(textb);
return 0;
}
Last edited by 7stud : March 30th, 2003 at 06:34 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|