pointer to first element in array

This is a discussion on pointer to first element in array within the C Programming forums, part of the General Programming Boards category; Hello, I have a very basic question, and this doesn't seem to be working for me: Heres a test code: ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    51

    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:

    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
    Posts
    1,528
    If you want it to point to myArray[0] then do this:

    Code:
    myptr = &myArray[0];

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Code:
    myptr = myArray;

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Code:
    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 further.so 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
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    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, 02:17 PM
  2. Replies: 7
    Last Post: 11-25-2008, 12: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, 05:58 PM

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