Thread: Read one and multiple bytes in function

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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

  2. #2
    Registered User
    Join Date
    Oct 2019
    Posts
    81
    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

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