C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-18-2009, 06:45 PM   #1
Registered User
 
Join Date: Nov 2009
Posts: 51
Send() not "sending" whole message

Code:
printf("\nPlease enter username : ");
					scanf( "%s", &username);
					printf("Please enter password : ");
					scanf( "%s", &password);
					send(sockfd, &username, strlen(&username), 0);
					break;
Can anyone see why this code is sending all but the first character?
Martin_T is offline   Reply With Quote
Old 11-18-2009, 06:49 PM   #2
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,230
How is "username" declared? Your use of & in all three cases here is not appropriate if these are just normal char pointers.
__________________

"A man can't just sit around." -- Larry Walters
MK27 is offline   Reply With Quote
Old 11-18-2009, 06:50 PM   #3
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,643
You don't need the & for scanning into an array / pointer. Also, you probably want to print your input just to be sure it's what you think it is:
Code:
scanf( ... )
printf( "debug: \'%s\'\n", username );

Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 11-18-2009, 06:50 PM   #4
cas
Registered User
 
Join Date: Sep 2007
Posts: 452
If your compiler is not complaining about strlen(&username), you've got your types wrong (or you forgot to #include <string.h>). If your compiler is complaining about strlen(&username), you should fix the problem; don't ignore warnings from a compiler, as they tend to be useful.

At the very least, you should remove all those ampersands. They cannot be right (except possibly the &username in send() is acceptable, but even then you can drop it). Without seeing all of your code, though, I can't definitively say what's going wrong. It'd be really helpful to see the declarations of username and password, at least.
cas is offline   Reply With Quote
Old 11-18-2009, 07:08 PM   #5
Registered User
 
Join Date: Nov 2009
Posts: 51
Code:
     char *username;
    char *password;
Code:
     printf("\nPlease enter your new username : ");
					scanf( "%s", username);
					printf("Please enter  your password : ");
					scanf( "%s", password);
					send(sockfd, username, strlen(username), 0);
					break;
When run like this, as soon as i press enter after inputting the user name I get a segmentation fault.
Martin_T is offline   Reply With Quote
Old 11-18-2009, 07:10 PM   #6
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,643
You didn't actually allocate space for either pointer, did you?


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 11-18-2009, 07:16 PM   #7
Registered User
 
Join Date: Nov 2009
Posts: 51
Sorry, I am quite new to C and have not quite got to grips with the pointer stuff.

Originally I had

Code:
     char username;
    char password;
Code:
     printf("\nPlease enter your new username : ");
					scanf( "%s", &username);
					printf("Please enter  your password : ");
					scanf( "%s", &password);
					send(sockfd, &username, strlen(&username), 0);
					break;
But that read in all the text, however long just missing of the first character.
Martin_T is offline   Reply With Quote
Old 11-18-2009, 07:21 PM   #8
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,643
Code:
char justonecharacter;
char *pointertoacharacterbutnoactualallocatedmemory;
char arrayofXelementsofactualallocatedspace[ X ];
If you're using a pointer, then you need to make it point some place. That means you need something like malloc.


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 11-18-2009, 07:32 PM   #9
Registered User
 
Join Date: Nov 2009
Posts: 51
Thanks! I've now declared them, for example :
Code:
     char string[50];
is this bad practice?
Martin_T is offline   Reply With Quote
Old 11-18-2009, 07:37 PM   #10
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,230
Quote:
Originally Posted by Martin_T View Post
is this bad practice?
No, but then you will want to restrict the amount of input in the scanf line so you cannot overflow -- scanf("%49s") or something.
__________________

"A man can't just sit around." -- Larry Walters
MK27 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Desperate Over Ecs! cookie C Programming 17 07-16-2008 01:25 PM
Message class ** Need help befor 12am tonight** TransformedBG C++ Programming 1 11-29-2006 11:03 PM
Making a script language? Blackroot Game Programming 10 02-16-2006 02:22 AM
pointers InvariantLoop C Programming 13 02-04-2005 09:32 AM
Sending CChildView a Message :: MFC kuphryn Windows Programming 0 04-06-2002 03:00 PM


All times are GMT -6. The time now is 05:21 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22