C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old March 24th, 2003, 07:59 PM
Ghetalion Ghetalion is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 7 Ghetalion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to Ghetalion
Angry 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.

Reply With Quote
  #2  
Old March 24th, 2003, 11:16 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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.

Reply With Quote
  #3  
Old March 24th, 2003, 11:21 PM
Ghetalion Ghetalion is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 7 Ghetalion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to Ghetalion
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

Reply With Quote
  #4  
Old March 25th, 2003, 12:52 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,430 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 21 h 41 m 55 sec
Reputation Power: 784
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

Reply With Quote
  #5  
Old March 25th, 2003, 03:42 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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.

Reply With Quote
  #6  
Old March 25th, 2003, 07:44 AM
3dfxMM 3dfxMM is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 266 3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 18 h 56 m 13 sec
Reputation Power: 12
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.

Reply With Quote
  #7  
Old March 25th, 2003, 06:22 PM
Ghetalion Ghetalion is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 7 Ghetalion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to Ghetalion
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.

Reply With Quote
  #8  
Old March 30th, 2003, 03:50 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
"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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > An Average programmer gone Novice trying to be Average again.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway