Thread: pointer to array

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    1

    Smile pointer to array

    hi
    please help me with the concept of pointer to an array and also macros.
    regards
    Mrunal

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    i am not an expert...but i KNOW that when we tell "there is apointer to an array we mean a pointer to the first element of the array, the first cell if you want".

    for example: if we have an array of integers size 10.
    Code:
    int A[10];
    and a pointer: *ptr
    Code:
    int *ptr;
    this part:
    Code:
     ptr = &A[10]
    simply "strores" to the pointer the beginning of the array. the A[0] cell.

    However what i told you is somehow general can you be more specific to what do you want to do?

  3. #3
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    Quote Originally Posted by brack View Post
    i am not an expert...but i KNOW that when we tell "there is apointer to an array we mean a pointer to the first element of the array, the first cell if you want".

    for example: if we have an array of integers size 10.
    Code:
    int A[10];
    and a pointer: *ptr
    Code:
    int *ptr;
    this part:
    Code:
     ptr = &A[10]
    simply "strores" to the pointer the beginning of the array. the A[0] cell.

    However what i told you is somehow general can you be more specific to what do you want to do?
    If you are having array A of size 10, you can access from A[0] to A[9], a[10] will be a garbage.

    Code:
    ptr = &A[10];
    here ptr will store address of A[10] cell, i.e. of 11th cell which will be having garbage.

    there are two ways
    1.
    Code:
     ptr=&A[0];
    2.
    Code:
      ptr=A;
    As in 2nd case, name of array gives base address of array i.e. A[0]

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    47
    Thanks ,everyone .I did it,this is my solution
    Code:
    void swapColumns(double a[][6],int row,int col,int fswap,int sswap)
     {
        int i;
        double hold,*px;
        px=a;
       for (i=0;i<row ;i++) 
       {  
        hold=*(px+i*col+fswap); 
        *(px+i*col+fswap)=*(px+i*col+sswap);
        *(px+i*col+sswap)= hold; 
       }  
      }

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    *(px+i*col+fswap);
    can be written as
    px[i*col+fswap]

    Much better, don't you think?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User l2krence's Avatar
    Join Date
    Nov 2010
    Posts
    24
    Hi,
    well,
    there's some problem with your coding...
    look this when i compile them >
    http://i235.photobucket.com/albums/e.../untitled5.jpg
    i don't think that px = a; is a valid command

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah, l2krence has a point. This is undefined behavior,
    A 2D array is not the same as a pointer.
    What I fail to see is why you don't just do
    a[i][fwap] = ... ;
    instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Replies: 0
    Last Post: 05-29-2009, 05:48 AM
  3. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  4. pointer to array of structs
    By Luken8r in forum C Programming
    Replies: 2
    Last Post: 01-08-2008, 02:05 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM