Thread: 4 layer reference model for communication error

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    1

    4 layer reference model for communication error

    I'm trying to simulate a simple 4 layer reference model for communication between two hosts. However, I seem to be getting a simple error, which I can't figure out.

    The Part of my code is:

    Code:
    int* layer2(int* buf,int* bufsize, int flag)
    {
    	if(flag == 1) //Transmit
    	{
    		int packetnum = 1;
    		
    		if(bufsize[0] > MAXDATA)
    		{
    			double x = (((double)bufsize[0])/MAXDATA);
    			packetnum = ceil(x);
    		}
    And the following error I'm getting refers to packetnum = ceil(x);

    [Warning] converting to `int' from `double'

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    That's not an error; it's a warning. It's only letting you know that you're going to lose precision converting from double to int. If that's actually what you want to do, do a typecast.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    36

    Type Casting

    In your program,you're trying to assign the double value to integer.That's why,it is giving warning.
    To avoid this,we need to explicitly do type conversion.

    In the case of operations,C itself manage it.For ex,if you're adding two integers and storing it in the double value,then the result will be implicitly converted to double value.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: converting double to int

    I tried your sample code with my sample inputs. It doesn't give any error or warning.
    Normally C will handle this automatic type conversion. ( double to int )

    Code:
    #include<stdio.h>
    #include<math.h>
    
    int* layer2(int* buf,int* bufsize, int flag)
    {
            if(flag == 1) //Transmit
            {
                    int packetnum = 1;
    
                    if(bufsize[0] > 10)
                    {
                            double x = (((double)bufsize[0])/10);
                            packetnum = ceil(x);
                            printf("packet:%d",packetnum);
                    }
            }
    }
    
    int main()
    {
    
            int buf[]={1,2,3};
            int bufsize[]={25,30,40};
            layer2(buf,bufsize,1);
            return 0;
    }
    Last edited by sganesh; 02-23-2010 at 05:57 AM. Reason: I missed one word

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM