Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. how to initialize a value to pointer ?

how to initialize a value to pointer ?

Scheduled Pinned Locked Moved ATL / WTL / STL
tutorialquestion
6 Posts 3 Posters 16 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    i tried adding values to different addresses of arrays by using pointer but it doesn't work.

    #include

    int main()
    {
    int i, num[10];
    int *p;
    p = &num[0];

    for(i=0; i<=10; i++)
    {
    	(p+i) = i;
    	printf("\\n%d", \*(p+i));
    }
    
    
    return 0;
    

    }

    R L 2 Replies Last reply
    0
    • T Tarun Jha

      i tried adding values to different addresses of arrays by using pointer but it doesn't work.

      #include

      int main()
      {
      int i, num[10];
      int *p;
      p = &num[0];

      for(i=0; i<=10; i++)
      {
      	(p+i) = i;
      	printf("\\n%d", \*(p+i));
      }
      
      
      return 0;
      

      }

      R Offline
      R Offline
      Rick York
      wrote on last edited by
      #2

      Inside your for loop it needs to have this :

      *(p+i) = i;

      (p+i) is a pointer incremented by i. You have to dereference the pointer to save a value to where it is aiming.

      T 1 Reply Last reply
      0
      • R Rick York

        Inside your for loop it needs to have this :

        *(p+i) = i;

        (p+i) is a pointer incremented by i. You have to dereference the pointer to save a value to where it is aiming.

        T Offline
        T Offline
        Tarun Jha
        wrote on last edited by
        #3

        if p = address of the num[0] then (p+i) is the address of the ith address and it is where i want to store the value, *(p+i) will give the value at that address i.e. de-referencing, below is the code i tried.

        #include

        int main()
        {
        int i, num[10];
        int *p;
        p = &num[0];

        for(i=0; i<=10; i++)
        {
        	\*(p+i) = i;
        }
        
        for(i=0; i<=10; i++)
        {
        	printf("\\n%d", \*(p+i));
        }
        
        
        return 0;
        

        }

        R 1 Reply Last reply
        0
        • T Tarun Jha

          if p = address of the num[0] then (p+i) is the address of the ith address and it is where i want to store the value, *(p+i) will give the value at that address i.e. de-referencing, below is the code i tried.

          #include

          int main()
          {
          int i, num[10];
          int *p;
          p = &num[0];

          for(i=0; i<=10; i++)
          {
          	\*(p+i) = i;
          }
          
          for(i=0; i<=10; i++)
          {
          	printf("\\n%d", \*(p+i));
          }
          
          
          return 0;
          

          }

          R Offline
          R Offline
          Rick York
          wrote on last edited by
          #4

          You are very close. The problem now is your for loops are exceeding the capacity of num. It has 10 elements and your loops go from 0 to 10 inclusively. That is 11 elements - there is no num[11] item so that is a very bad thing. I don't like literal values so I made the number a const and adjusted the loops :

          const int Count = 10;

          void main()
          {
          int i;
          int num[Count];
          int *p;

          p = &num\[0\];
          
          for( i=0; i < Count; i++ )
          {
          	\*(p+i) = i;
          }
          
          for( i=0; i < Count; i++ )
          {
          	trace( \_T( "item %d is %d" ), i, \*(p+i) );
          }
          return 0;
          

          }

          Your code looks quite a bit like C but this is a section for ATL/WTL/STL is about C++. If you are actually using C then this code won't compile. You can make Count a macro definition and then it will.

          T 1 Reply Last reply
          0
          • T Tarun Jha

            i tried adding values to different addresses of arrays by using pointer but it doesn't work.

            #include

            int main()
            {
            int i, num[10];
            int *p;
            p = &num[0];

            for(i=0; i<=10; i++)
            {
            	(p+i) = i;
            	printf("\\n%d", \*(p+i));
            }
            
            
            return 0;
            

            }

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            See Re: Recursion not working. - ATL / WTL / STL Discussion Boards[^]. You obviously have still not understood pointers and how to use them, so I say again, get a good book on C and study it hard.

            1 Reply Last reply
            0
            • R Rick York

              You are very close. The problem now is your for loops are exceeding the capacity of num. It has 10 elements and your loops go from 0 to 10 inclusively. That is 11 elements - there is no num[11] item so that is a very bad thing. I don't like literal values so I made the number a const and adjusted the loops :

              const int Count = 10;

              void main()
              {
              int i;
              int num[Count];
              int *p;

              p = &num\[0\];
              
              for( i=0; i < Count; i++ )
              {
              	\*(p+i) = i;
              }
              
              for( i=0; i < Count; i++ )
              {
              	trace( \_T( "item %d is %d" ), i, \*(p+i) );
              }
              return 0;
              

              }

              Your code looks quite a bit like C but this is a section for ATL/WTL/STL is about C++. If you are actually using C then this code won't compile. You can make Count a macro definition and then it will.

              T Offline
              T Offline
              Tarun Jha
              wrote on last edited by
              #6

              thank you. And here is the correct code.

              #include

              int main()
              {
              int i, num[10];
              int *p;
              p = &num[0];

              for(i=0; i<10; i++)
              {
              	\*(p+i) = i;
              }
              
              for(i=0; i<10; i++)
              {
              	printf("\\n%d", \*(p+i));
              }
              
              
              return 0;
              

              }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups