Like Tree1Likes
  • 1 Post By Salem

A very stupid question on pointers

This is a discussion on A very stupid question on pointers within the C Programming forums, part of the General Programming Boards category; i want to know why dont we prefix the pointer with * when using it on dynamic array. Code: #include ...

  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
    21,987
    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].)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    *ptr could be written as *(ptr+0), which is the same as writing ptr[0]
    vart likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,251
    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, 05:15 PM
  3. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 07: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, 06:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21