Pointer Arithmetic Problem

  • From: "ali_uk" < >
  • To:
  • Subject: Pointer Arithmetic Problem
  • Date: Fri, 01 Feb 2013 19:25:11 -0500

Hello Paul and thank you for explaining that so well.  I appreciate your time.

I corrected the code according to your advice and the result is excellent.  
Here are the key lines corrected:

Code:

    // this goes into the first slot for the 5 integers. Each slot is said to 
be 
    // 4 bytes wide, but we can't rely on that, so we use sizeof
    *ptr_one = 78;
    *(ptr_one + sizeof(int)*1) = 94; // this goes into the second slot 
    *(ptr_one + sizeof(int)*2) = 22; // this goes into the third slot 
    *(ptr_one + sizeof(int)*3) = 56; // this goes into the fourth slot 

    // this goes into the first slot for the 10 characters. Each slot is said 
to be
    // 4 bytes wide, but we can't rely on that, so we use sizeof
    *ptr_two = 'A';
    *(ptr_two + sizeof(char)*1) = 'B'; // this goes into the second slot 
    *(ptr_two + sizeof(char)*2) = 'C'; // this goes into the third slot 
    *(ptr_two + sizeof(char)*3) = 'D'; // this goes into the fourth slot 


    printf("%p contains %p\n", &ptr_one, ptr_one);
    printf("%p contains %d\n", &ptr_one[0], *ptr_one);
    printf("%p contains %d\n", &ptr_one[1], *(ptr_one + sizeof(int)*1));
    printf("%p contains %d\n", &ptr_one[2], *(ptr_one + sizeof(int)*2));
    printf("%p contains %d\n", &ptr_one[3], *(ptr_one + sizeof(int)*3));
    printf("\n");
    printf("%p contains %p\n", &ptr_two, ptr_two);
    printf("%p contains %c\n", &ptr_two[0], *ptr_two);
    printf("%p contains %c\n", &ptr_two[1], *(ptr_two + sizeof(char)*1));
    printf("%p contains %c\n", &ptr_two[2], *(ptr_two + sizeof(char)*2));
    printf("%p contains %c\n", &ptr_two[3], *(ptr_two + sizeof(char)*3));





Thank you again.  :D
Ali.






Pointer Arithmetic Problem

ali_uk 02/01/2013

Re: Pointer Arithmetic Problem

Paul Floyd 02/01/2013

Pointer Arithmetic Problem

ali_uk 02/02/2013

Project Features

About this Project

CND was started in November 2009, is owned by Leonid Lenyashin, and has 130 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20131025.e7cbc9d). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close