Thread: Read one and multiple bytes in function

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    68

    Read one and multiple bytes in function

    Hello

    I need help in my embedded project. assume array is loaded with five bytes so we know the location of each byte and data stored in the location.

    byte 1 = location for byte1 6422296 = 00
    byte 2 = location for byte2 6422300 = 01

    byte 3 = location for byte3 6422304 = 02
    byte 4 = location for byte4 6422308 = 03
    byte 5 = location for byte5 6422312 = 04


    I need to design two function

    *read one byte
    *read five bytes

    In the read one byte function, function would take the memory location of one byte and save result

    in read multiple byte function, read five bytes by calling read one byte function

    I made my attempt

    Code:
    #include<stdio.h> 
    // Function to read one byte 
    void ReadByte (int *N)
    {
         //loop to read 8 bits 
        for ( n = 0; n <8; n++ )     
        {
           /*read each bit of one byte */
        }
    }
     
    // Function to read five byte  
    void ReadMultipleByte (int *P )
    {
        //loop to read 5 bytes
        for ( n = 0; n <5; n++ )
        {
    	   // call the function to read one byte and it will run five time in loop
            
        }
    }
    void main ()
    {
    	int n;
        int Data[]= {0,1,2,3,4}; //five bytes are stored into array 
    	
    	ReadMultipleByte (&Data)
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,986
    Well you should change your data type from 'int' to 'unsigned char' to begin with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    68
    Quote Originally Posted by Salem View Post
    Well you should change your data type from 'int' to 'unsigned char' to begin with.
    I have done
    Code:
    #include<stdio.h> 
    // Function to read one byte 
    void ReadByte (unsigned char *N)
    {
         //loop to read 8 bits 
        for ( n = 0; n <8; n++ )     
        {
           /*read each bit of one byte */
        }
    }
     
    // Function to read five byte  
    void ReadMultipleByte (unsigned char *P )
    {
        //loop to read 5 bytes
        for ( n = 0; n <5; n++ )
        {
    	   // call the function to read one byte and it will run five time in loop
            
        }
    }
    void main ()
    {
    	int n;
        unsigned char Data[]= {0,1,2,3,4}; //five bytes are stored into array 
    	
    	ReadMultipleByte (&Data[0])
        return 0;
    }
    Is my logic correct ?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,986
    Looks better.

    Now just complete the code for the comments.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2019
    Posts
    68
    Quote Originally Posted by Salem View Post
    Looks better.

    Now just complete the code for the comments.
    My attempt

    Code:
    #include<stdio.h> 
    
    // Function to read one byte 
    void ReadByte (unsigned char *byteAddress)
    {
         //loop to read 8 bits 
        for ( n = 0; n <8; n++ )     
        {
    		 if ( condition1 ) 
                 *byteAddress |= 1 ; 
    		 
             if ( n < 7 ) 
                 *byteAddress <<= 1 ;   
        }
    	
    }
      
    // Function to read five byte  
    void ReadMultipleByte ( unsigned char *byteAddress)
    {
    	int n;
    	unsigned char R;
        //loop to read 5 bytes
        for ( n = 0; n <5; n++ )   
        {
          R =  ReadByte( unsigned char *byteAddress)    // call the function to read one byte and it will run five time in loop
    	  ++byteAddress;
        
        }
    }
    void main ()
    {
        int n;
        unsigned char Data[]= {0,1,2,3,4}; //five bytes are stored into array 
         
        ReadMultipleByte ( Data])
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Oct 2019
    Posts
    68
    Quote Originally Posted by Salem View Post
    Looks better.
    I amtrying to fix some issues

    why code doesn't print the correct value stored in the first memory location of array

    Code:
    #include<stdio.h> 
    
    int read (unsigned int N, unsigned int *p);
    
    
    int read (unsigned int N, unsigned int *p)
    {
    	N = 5;
    	
    	for ( N = 5; N > 0; --N)
    	{
    		printf("\n read address %p \n value store at this address %d", (p + N), *( p + N));
    	}
    	
    }
    
    
    int main ()
    {
    	int s;
        int i = 5 ;
        int array[]= {20, 18, 21, 45, 42 };
    	
        for (i = 5; i>0; --i)
    	{
    		printf ("%p  = %d \n", &array[i], array[i]);
    	}		
      
    	s= read (i, array);
    	
       return 0;
    }
    0061FF28 = 3203072
    0061FF24 = 42
    0061FF20 = 45
    0061FF1C = 21
    0061FF18 = 18


    read address 0061FF28
    value store at this address 3203072
    read address 0061FF24
    value store at this address 42
    read address 0061FF20
    value store at this address 45
    read address 0061FF1C
    value store at this address 21
    read address 0061FF18
    value store at this address 18
    Last edited by gajya; 01-05-2020 at 12:59 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,748
    Just stop for a moment and ask yourself: in an array of 5 elements, is array[5] valid?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2019
    Posts
    68
    following approach work for me

    Code:
    #include<stdio.h>    
    
    int read (unsigned int N, unsigned int *p);
      
    int read (unsigned int N, unsigned int *p)
    {
        int i;
    	   for (int i = N-1; i >= 0; i--) {   
    	   printf("\n read address %p \n ", (p + N-1));   
           printf("value store at this address is = %d \n", *( p + N-1));
           --p; 	   
        }    
    }
     
         
    int main()    
    {    
        int s;  int N = 5 ;  // length of array     
        
        //Initialize array     
        int Data[] = {1, 2, 3, 4, 5};     
             
        printf("Original array: \n");    
        for (int i = 0; i < N; i++) {  
            printf("%p  = %d \n", &Data[i], Data[i] ); 	 
        }      
            
        printf("\n");    
            
        printf("Array in reverse order: \n");    
        //Loop through the array in reverse order    
        for (int i = N-1; i >= 0; i--) {   
            printf("%p  = %d \n", &Data[i], Data[i] ); 	 
        }
         s= read (N, Data);	
        return 0;    
    }
    Original array:
    0061FF0C = 1
    0061FF10 = 2
    0061FF14 = 3
    0061FF18 = 4
    0061FF1C = 5


    Array in reverse order:
    0061FF1C = 5
    0061FF18 = 4
    0061FF14 = 3
    0061FF10 = 2
    0061FF0C = 1


    read address 0061FF1C
    value store at this address is = 5


    read address 0061FF18
    value store at this address is = 4


    read address 0061FF14
    value store at this address is = 3


    read address 0061FF10
    value store at this address is = 2


    read address 0061FF0C
    value store at this address is = 1

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,986
    > int i;
    > for (int i = N-1; i >= 0; i--)
    There's no point having the first declaration of i.

    > printf("\n read address %p \n ", (p + N-1));
    Try
    printf("\n read address %p \n ", (p + i));

    And stop messing about with decrementing p with p--
    Since p points to the start of the array (in main), you're decrementing to somewhere before the beginning of the array.

    Regardless, it's an invalid pointer.
    Question 6.17
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do i read two bytes in a row?
    By Mohsen Abasi in forum C Programming
    Replies: 2
    Last Post: 04-23-2017, 11:54 AM
  2. Replies: 6
    Last Post: 02-08-2012, 06:54 PM
  3. bytes lost with partial read in UDP
    By mynickmynick in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-31-2009, 02:06 AM
  4. read 2 bytes from a file...
    By compile in forum C Programming
    Replies: 10
    Last Post: 10-17-2006, 12:54 AM
  5. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM

Tags for this Thread