Thread: Question about array pointer

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    26

    Question Question about array pointer

    Hi all,

    I am a bit confused on how to use array pointers.

    What I want to do is to fill a predefined region starting from a known memory location.

    I thought of doing something like that:


    Code:
    /*
     * I want to fill the region from
     * [0xa00000000 - 0xA00000400) with zeros
     */
    
    uint32_t *Array_A[100] = (uint32_t*)0xA000000;
    
    
    for (count=0; count<100; count++)
    {
            Array_A[count]=0;
    }
    Unfortunately, the above doesn't seem to work as expected.

    Could anyone tell me what I am doing wrong?


    Thanks a lot in advance.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Pointing pointers at random spots in memory is usually a bad idea. It hasn't been a really good idea for 15 years or more. Anyway, you have an array of pointers, and you are trying to assign the whole thing a value at once, which you can't do.
    Code:
    int array[ 100 ] = 5;
    Just like that's wrong for non-pointer arrays, it's still wrong for arrays of pointers. You would have to do something like:
    Code:
    foo *bar[ BAZ ] = { VALUE };
    Which would only set the first value to what you want, and everything after that would be set to zero.

    Why don't you just use something like memset if you want to specify a value to copy everywhere? Otherwise, if you want a range of values, you will have to loop through it on your own.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    26
    Hi and thanks for you reply

    Quote Originally Posted by quzah View Post
    Pointing pointers at random spots in memory is usually a bad idea. It hasn't been a really good idea for 15 years or more.
    It's not really random, I know which memory region to use but in general you're right.

    Quote Originally Posted by quzah View Post
    Anyway, you have an array of pointers, and you are trying to assign the whole thing a value at once, which you can't do.
    I don't really get where I am trying to do that...

    With that

    Code:
    uint32_t *Array_A[100] = (uint32_t*)0xA000000;
    I am just making the array pointer to point at 0xA000000 and then I am trying to set a single location at a time with the loop.

    Quote Originally Posted by quzah View Post
    Why don't you just use something like memset if you want to specify a value to copy everywhere?
    I don't actually have lib support and I would like to do it just with array pointers if possible.

    Thanks.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    uint32_t *Array_A[100] = (uint32_t*)0xA000000;
    I am just making the array pointer to point at 0xA000000 and then I am trying to set a single location at a time with the loop.
    Unless you are working on an imbedded system and that's a microcontroller addres this is a Bad Idea... a real bad idea!

    If you're just trying to build an array... use malloc() that's what it's for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer Array Question
    By d320 in forum C Programming
    Replies: 5
    Last Post: 07-11-2008, 11:51 PM
  2. array pointer question
    By Hoser83 in forum C Programming
    Replies: 5
    Last Post: 02-03-2006, 11:19 AM
  3. pointer/array question
    By saller in forum C Programming
    Replies: 8
    Last Post: 01-10-2006, 09:39 PM
  4. Array/Pointer question
    By Swaine777 in forum C++ Programming
    Replies: 2
    Last Post: 05-17-2003, 10:29 PM
  5. A question between Array and Pointer
    By Unregistered in forum C++ Programming
    Replies: 16
    Last Post: 08-05-2002, 09:13 AM