Pointer Arithmetic Problem

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

Hello All

I am learning about pointers in C.

Can someone please tell me why I get 67 instead of 22 when I run the code 
below?  
The code compiles and runs OK.  All the other numbers and letters print 
correctly as expected.

Thank you 


Code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv)
    {
            // Declare pointers
                int* ptr_one; // This points to an int
                char* ptr_two; // This points to a string

        // allocate space for 5 integers and assign to pointer 1
                ptr_one = (int *)malloc(sizeof(int)*5); 

        // allocate space for 10 characters and assign to pointer 2
                ptr_two = (char *)malloc(sizeof(char)*10); 
                
        //check for correct space allocation
                if (ptr_one == 0 || ptr_two == 0)
                {
                        printf("ERROR: memory allocation problem\n");
                        return 1;
                }

        // this goes into the first slot for the 5 integers. Each slot is 4 
bytes wide
                *ptr_one = 78; 
                *(ptr_one + 4) = 94;// this goes into the second slot 
                *(ptr_one + 8) = 22;// this goes into the third slot 
                *(ptr_one + 12) = 56;// this goes into the fourth slot 
                
        // this goes into the first slot for the 10 characters. Each slot is 
4 bytes wide
                *ptr_two = 'A';
                *(ptr_two + 4) = 'B';// this goes into the second slot 
                *(ptr_two + 8) = 'C';// this goes into the third slot 
                *(ptr_two + 12) = 'D';// this goes into the fourth slot 
                
                
                printf("%p contains %p\n", &ptr_one, ptr_one);// prints the 
address for ptr_one[0]
                printf("%p contains %d\n", &ptr_one[0], *ptr_one);// prints 78
                printf("%p contains %d\n", &ptr_one[1], *(ptr_one+4));// 
prints 94
                printf("%p contains %d\n", &ptr_one[2], *(ptr_one+8));// 
prints 67
                printf("%p contains %d\n", &ptr_one[3], *(ptr_one+12));// 
prints 56
                printf("\n");
                printf("%p contains %p\n", &ptr_two, ptr_two); // prints the 
address for ptr_two[0]
                printf("%p contains %c\n", &ptr_two[0], *ptr_two);// prints A
                printf("%p contains %c\n", &ptr_two[1], *(ptr_two+4));// 
prints B
                printf("%p contains %c\n", &ptr_two[2], *(ptr_two+8));// 
prints C
                printf("%p contains %c\n", &ptr_two[3], *(ptr_two+12));// 
prints D

        // Free the memory
                free(ptr_one);
                free(ptr_two);

    return (EXIT_SUCCESS);
    }










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 20140418.2d69abc). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close