Thread: A very stupid question on pointers

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    9

    Question A very stupid question on pointers

    i want to know why dont we prefix the pointer with * when using it on dynamic array.

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    void main()
    {
    
    int i;
    int k=10;
    
    int *normalpointer;
    normalpointer=&k;
    printf("*normalpointer = %d (prefix the pointer with * here)\n", *normalpointer);
    
    int *ptr;  //dynamic array pointer
    ptr=(int *)malloc(sizeof(int)*10);   //array size 10
    printf("Dynamic array created of size 10\n");
    printf("printing content without prefixing the pointer by * \n");
    for(i=0;i<10;i++)
    {
     ptr[i]=i+1;   //no * prefix
     printf("%d  ",ptr[i]);  // *ptr[i] is illegal why ??
    }
    
    }

    I dont have any problem in coding but i just need to know whats the theory going on here.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Given a pointer p and an integer n, p[n] is equivalent to *(p + n). (... which is equivalent to *(n + p), which is equivalent to n[p].)
    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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    *ptr could be written as *(ptr+0), which is the same as writing ptr[0]
    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.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by max1989 View Post
    i want to know why dont we prefix the pointer with * when using it on dynamic array.
    Code:
    printf("*normalpointer = %d (prefix the pointer with * here)\n", *normalpointer);
    Here you are "dereferencing" the pointer to get to the value it points to. *normalpointer in this case returns an integer.

    Code:
    int *ptr;  //dynamic array pointer
    ...
    printf("%d  ",ptr[i]);  // *ptr[i] is illegal why ??
    }
    In this case the pointer is dereferenced by the [] brackets. ptr[i] is the same as *(ptr+i) ... go ahead try it...

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    9
    *(ptr+i) worked. THANKS

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by max1989 View Post
    *(ptr+i) worked. THANKS
    *smack head on desk*

    There was nothing wrong with it before. I think I heard the sound of your hair parting from a distance of six light years due to concepts flying over your head
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very stupid question about pointers
    By Sink0 in forum C Programming
    Replies: 7
    Last Post: 10-21-2010, 10:30 AM
  2. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 06:15 PM
  3. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 08:22 PM
  4. rfgrgf, stupid pointers
    By jverkoey in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2003, 07:22 PM
  5. Stupid Math Question....really stupid
    By ToLazytoSignIn in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 01-16-2003, 07:36 PM