Thread: need help in pointers.

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    4

    need help in pointers.

    Hi All,

    I am new to C language. I read this code somewhere but I am not able to understand how this is working. Kindly clear some doubts. Please see my comments where I have doubts

    float linearA[30];
    float *A[6];

    [Pawan] What is the actually interpretation of this declaration?




    A[0] = linearA; /* 5 - 0 = 5 elements in row */
    A[1] = linearA + 5; /* 11 - 5 = 6 elements in row */
    A[2] = linearA + 11; /* 15 - 11 = 4 elements in row */
    A[3] = linearA + 15; /* 21 -15 = 5 elements */
    A[4] = linearA + 21; /* 25 - 21 = 4 elements */
    A[5] = linearA + 25; /* 30 - 25 = 5 elements */


    [Pawan] Please explain this code, how this is allocating different length of rows?


    Thanks
    Pawan.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    This might clear things up:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int
    main (int argc, char **argv)
    {
      float *p[3];
      float array[5] = {
        0.1, 0.2, 0.3, 0.4, 0.5
      };
    
      p[0] = array;   /* point to the 1st element of array[5] */
      p[1] = array+1; /* point to the 2nd element of array[5] */
      p[2] = array+4; /* point to the 4th element of array[5] */
    
      printf (
          "PRINTED VALUES USING *(p[n])\n"
          "1st: %f\n"
          "2nd: %f\n"
          "4th: %f\n\n",
          *(p[0]), *(p[1]), *(p[2])
          );
    
      printf (
          "PRINTED VALUES USING array[n]\n"
          "1st: %f\n"
          "2nd: %f\n"
          "4th: %f\n",
          array[0], array[1], array[3]
          );
    
      return 0;
    }
    A *pointer is simple a variable holding an memory-address where there might be data for you to read/alter. As said: just by using a pointer - you are not allocating any new memory.

    The comments in the code you wrote are very misleading, but I think that the author intended to say that pointer A[0] will point to an address 5 elements from A[1]. A[1] on the other hand will point to an address 6 elements from A[2], and so on.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Thanks alot! I think the way you have explained is best. once again thanks alot If I have understood it correctly

    when you have p[1] = array+1; /* point to the 2nd element of array[5] */

    you can use it like p[1]++ ?

    Also the example I have given is used when We want to have an two dimensional array, but we don't need to have all the rows the same length. Is this the way to use it?


    Regards,
    Pawan Chopra.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    I'm not sure of if I understand your question, but if you increment your pointer by one you will have it pointing towards the next element, example:

    Code:
    #include <stdio.h>
    
    int
    main (int argc, char **argv)
    {
      int array[] = {1,2,3};
      int *p;
    
      p = array; /* point p towards the first element */
      (*p)++; /* increment value pointed to by p by 1 */
      p++; /* increment pointer by one, making it point to the next element in array[] */
    
      printf ("p = %p, *p = %d\n\n", p, *p);
      printf ("[%p] array[%d] = %d\n", &(array[0]), 0, array[0]);
      printf ("[%p] array[%d] = %d\n", &(array[1]), 1, array[1]);
      printf ("[%p] array[%d] = %d\n", &(array[2]), 2, array[2]);
    
      return 0;
    }
    inside the square-brackets you will have the address of values. You will see that the address which p is pointing towards will be the same as the address of array[1].
    (The address-of operator '&' is exactly what it says it is - an operator the get the address of some variable:

    Code:
    int  x = 5;
    printf("addr: %p value: %d\n", &x, x);

    Quote Originally Posted by pawanspace
    Also the example I have given is used when We want to have an two dimensional array, but we don't need to have all the rows the same length. Is this the way to use it?
    To me that sounds like bad coding or bad problem-solving, and in terms of memory there are no rows (etc).
    If you think that it will be easier to manage your linear array of numbers by treating it as a matrix with rows and cols that is up to you, but I wouldn't write my code like that.
    Last edited by edoceo; 03-17-2009 at 05:11 AM. Reason: typo

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Thanks! Well I have just started with C language I have been studying K&R, but finding it complicated and hard to understand. Can you suggest me a good book for C language which should be for both novice and experienced people. Thanks!

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Quote Originally Posted by pawanspace View Post
    Thanks! Well I have just started with C language I have been studying K&R, but finding it complicated and hard to understand. Can you suggest me a good book for C language which should be for both novice and experienced people. Thanks!
    I'm sorry but I haven't read any C-book in my life, so I got nothing to recommend. For me trial and error has worked fine, and I also think that it is the best way of learning.I'm not saying that there aren't any good books out there though, just that I don't have enough experience to come with a conclusion - really.

    There are many tutorials describing both pointers and many other aspects of C, so give yourself a few minutes googling and I bet you find something satisfying.

    Your questions will also be answers if you write here at this forum, so even if you don't find any good book - I'm sure you will be alright!

    edit:

    Check out this thread for book recommendations: "C book recommendations"
    Last edited by edoceo; 03-17-2009 at 11:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM