Thread: pointer to first element in array

  1. #1
    Registered User
    Join Date
    Dec 2007

    pointer to first element in array

    Hello, I have a very basic question, and this doesn't seem to be working for me:

    Heres a test code:

    #include <stdio.h>
    int main()
       unsigned char *myptr;
       unsigned char myArray[256];
       // assign values to myArray here
       myptr = &myArray;
       return 0;
    Ok, Id like myptr to point to the memory address of myArray[0] but for some reason it doesn't work. Is my code right? Thanks.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    If you want it to point to myArray[0] then do this:

    myptr = &myArray[0];

  3. #3
    Registered User
    Join Date
    Oct 2008
    myptr = myArray;

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    in front of my computer
    myptr = &myArray;
    when u do this u're assigning the address of the whole array(which is of course the address of the first element).but this will create problem if u increment myptr the better way is to assign myptr as given by kermit or itCbitC.(both are same as the name of the array is same as its base address).

  5. #5
    Join Date
    Oct 2007
    Inside my computer
    There is such a thing as a pointer to the first element and a pointer to an array.
    The former requires you to assign the array itself, not take its address.
    The latter requires you to assign the address of the array.
    They are two different types and are treated differently by C/C++.
    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. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  3. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  4. Pros pls help, Pointer to array
    By kokopo2 in forum C Programming
    Replies: 7
    Last Post: 08-17-2005, 11:07 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM