Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-22-2012, 12:16 AM
|
#1
|
|
Member
Registered: May 2007
Posts: 135
Rep:
|
Bsic C programming questions
Hi all
This is *not* a serious program, i'm just learning to code. However it does illustrate something that i need to learn how to do.
If i have:
Code:
int x, y;
printf("Enter X,Y coordinates for your move: ");
scanf("%d%*c%d", &x, &y);
Then how do i send the variables x and y and the character between the two into a buffer for a call to write, in order to write to a socket, so that a host at the other end can just read the buffer at the other end?. The bit that tripped me up was that there's two data types. What is the correct way of doing it as:
Code:
char buff[3];
buff[0]=(char *) x;
buff[1]=' ';
buff[2]=(char *) y;
write(sockfd, buff, sizeof(buff));
will obviously not work, due to the incomplete type of char.
Thankx for helping a complete C beginner!
regards
Last edited by methodtwo; 07-22-2012 at 12:59 AM.
|
|
|
|
07-22-2012, 01:02 AM
|
#2
|
|
Member
Registered: May 2007
Posts: 135
Original Poster
Rep:
|
At the moment i have(for the whole function):
Code:
void get_player_move(void)
{
int num = 3;
char *buff = malloc(sizeof(int)*3);
int x, y;
printf("Enter X,Y coordinates for your move: ");
scanf("%d%*c%d", &x, &y);
x--; y--;
if(matrix[x][y] != ' '){
printf("Invalid move, try again.\n");
get_player_move();
}
/*add code to send move to server*/
else matrix[x][y] = 'X';
buff[0]= (char)x;
buff[1]=' ';
buff[2]= (char)y;
write(sockfd, buff, sizeof(buff));
}
But GCC is still com[plaining about dereferencing pointer to incomplete type!
And YES i still get the same error when declaring an int array and casting ' ' to an int like so:
int buff[3];
buff[0]= x;
buff[1]= (int)' ';
buff[2]= y;
Or using (int *) instead of (int)!
Thankx again
Last edited by methodtwo; 07-22-2012 at 01:10 AM.
|
|
|
|
07-22-2012, 01:16 AM
|
#3
|
|
Member
Registered: May 2007
Posts: 135
Original Poster
Rep:
|
I realize that i could just move the problem to the other end(the server) but then i'd still have to do similar on the other end. Is the only solution to just make the program not have a space between the 2 coordinates?
I i do now have some code that solves it this way and uses the non-deprecated socket functions i.e getaddrinfo.
However, just out of interest in C;the way to solve the problem first mentioned is to cast the variable that doesn't fit in the array, is that the only way?
Last edited by methodtwo; 07-22-2012 at 02:46 AM.
|
|
|
|
07-22-2012, 02:46 AM
|
#4
|
|
Senior Member
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 2,802
|
I think it is overcomplicated and unsafe:
scanf (or the program) will die if you enter some "strange" data. You would better check if the input was valid.
You do not need to send the separator, I think the server only need the value of x and y.
Also you have a variable num at the beginning which is not used at all.
this part of the code:
Code:
if(matrix[x][y] != ' '){
printf("Invalid move, try again.\n");
get_player_move();
will make a call to the function itself, it is not really nice, you should better write a loop:
Code:
do
sscanf(...)
....
while (matrix[x][y] != ' ')
(obviously it is not complete, you need to make it work).
I would use -Wall for gcc, I suggest you to not use any deprecated construct, also avoid warnings if possible. Do not mix int and int *.
|
|
|
|
07-22-2012, 06:38 AM
|
#5
|
|
Senior Member
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 4,674
|
Quote:
Originally Posted by methodtwo
However, just out of interest in C;the way to solve the problem first mentioned is to cast the variable that doesn't fit in the array, is that the only way?
|
The correct way to mix data types in an object is using a struct rather than an array. You can directly write/read the contents of a struct across the socket as long as the programs on both ends are compiled with compatible compilers/architectures. I expect you are using identical compilers/architectures on the two ends.
I don't understand how the error message you quoted could fit the portion of your code that you asked about. Using (int*) or (char*) when you needed (int) or (char) should give you an error message, but not the one you quoted.
You didn't provide enough code for me to try compiling it myself and see where that error message is coming from.
|
|
|
|
07-22-2012, 06:40 AM
|
#6
|
|
Member
Registered: May 2007
Posts: 135
Original Poster
Rep:
|
Thanks for a good reply to an embarrassing trifle matie. Yes it did get cleaned up a bit by not using deprecated syntax. I'll post what i've got so far, if anyone doesn't mind(can be bothered, that is...it might be too much trouble for an app that's purely for my learning purposes...messing around @home etc)
Thankx again
regards
Last edited by methodtwo; 07-22-2012 at 06:44 AM.
|
|
|
|
07-22-2012, 09:35 AM
|
#7
|
|
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,780
|
Maybe the below code helps to place the two integers in a (unsigned) character buffer; note that this is based on 32 bit integers.
Code:
#include <stdio.h>
#include <string.h>
int main()
{
int x, y;
unsigned char buffer[20];
int cnt;
printf("sizeof(int) = %d\n",sizeof(int));
printf("sizeof(char) = %d\n",sizeof(char));
printf("Enter X,Y coordinates for your move: ");
scanf("%d%*c%d", &x, &y);
memset(buffer, 0x00, 20);
buffer[0]=(x&0xFF000000)>>24;
buffer[1]=(x&0x00FF0000)>>16;
buffer[2]=(x&0x0000FF00)>>8;
buffer[3]=(x&0x000000FF);
buffer[4]=0x20;
buffer[5]=(y&0xFF000000)>>24;
buffer[6]=(y&0x00FF0000)>>16;
buffer[7]=(y&0x0000FF00)>>8;
buffer[8]=(y&0x000000FF);
for(cnt=0;cnt<20;cnt++)
{
printf("%02X ",buffer[cnt]);
if((cnt+1)%10==0)
{
printf("\n");
}
}
return 0;
}
The receiving side can reverse the process and get the correct data.
The alternative can be the use of unions.
Code:
#include <stdio.h>
#include <string.h>
int main()
{
union MyUnion
{
int i;
char c[sizeof(int)];
} x1, y1;
int x, y;
unsigned char buffer[20];
int cnt;
printf("Enter X,Y coordinates for your move: ");
scanf("%d%*c%d", &x, &y);
x1.i=x;
y1.i=y;
memset(buffer, 0x00, 20);
memcpy(&buffer[0],x1.c,sizeof(int));
memcpy(&buffer[0+sizeof(int)],y1.c,sizeof(int));
for(cnt=0;cnt<20;cnt++)
{
printf("%02X ",buffer[cnt]);
if((cnt+1)%10==0)
{
printf("\n");
}
}
return 0;
}
The advantage with this code is that it's independent of the size of the integer. The big disadvantage is that this assumes that both the client and the server run on the same architecture (e.g. Intel X86 family processor versus Motorola 68K family processor); read up on little endian / big endian); not an issue if both server and client run on the same machine. The solution is to convert to network byte order; read 'man htonl'.
Note the difference in the output of the 2 programs. Assuming a 32 bit integer, the bytes will be swapped around when using an Intel/AMD processor.
Last edited by Wim Sturkenboom; 07-22-2012 at 09:37 AM.
|
|
|
|
07-23-2012, 12:55 AM
|
#8
|
|
Member
Registered: Jul 2012
Location: Noida, India
Distribution: CentOS & Ubuntu
Posts: 68
Rep: 
|
If I were, I would prefer to write string to the socket and manipulate by separating it from ',' at server side.
char buf[16];
snprintf(buf,sizeof(buf),"%d,%d",x,y);
|
|
|
|
| Thread Tools |
Search 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
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:21 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|