LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 07-22-2012, 12:16 AM   #1
methodtwo
Member
 
Registered: May 2007
Posts: 146

Rep: Reputation: 18
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.
 
Old 07-22-2012, 01:02 AM   #2
methodtwo
Member
 
Registered: May 2007
Posts: 146

Original Poster
Rep: Reputation: 18
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.
 
Old 07-22-2012, 01:16 AM   #3
methodtwo
Member
 
Registered: May 2007
Posts: 146

Original Poster
Rep: Reputation: 18
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.
 
Old 07-22-2012, 02:46 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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 *.
 
Old 07-22-2012, 06:38 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by methodtwo View Post
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.
 
Old 07-22-2012, 06:40 AM   #6
methodtwo
Member
 
Registered: May 2007
Posts: 146

Original Poster
Rep: Reputation: 18
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.
 
Old 07-22-2012, 09:35 AM   #7
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
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.
 
Old 07-23-2012, 12:55 AM   #8
piyush.sharma
Member
 
Registered: Jul 2012
Location: Delhi, India
Distribution: CentOS
Posts: 82

Rep: Reputation: Disabled
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);
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
2 programming questions hk_michael Programming 4 02-01-2005 12:02 AM
programming questions eboats Linux - General 19 10-22-2001 05:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration