Thread: Array and other "Special pointers"

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137

    Question Array and other "Special pointers"

    C continues to fascinate me because although it is a relatively "small language" in regards to number of constructs/syntax, there are always some edge cases I haven't yet needed to use such as "multidimensional arrays" which I've just needed to start using.

    Question: In this program:
    Code:
    int main(void)
    {
         int *heap_mem = malloc(25*sizeof(int));
        int (*a)[5][5]; // 25 spaces that a points to.
        // [] [] [] [] []
        // [] [] [] [] []
        // [] [] [] [] []
        // [] [] [] [] []
        // [] [] [] [] []
    
    
        *a[0][0] = 1337;
    
    
        printf("%d\n", *a[0][0]);
    
    
        return EXIT_SUCCESS;
    }
    If I remove the ( and ) from (*a) then this program creates a segmentation fault. Why? More importantly, from my C experience thus far, I've noticed different pointer formats. So for example on line 3 here int *heap_mem = malloc(25*sizeof(int)); is your standard char * var_name pointer time we're all familiar with.

    However, then there are function pointers declared like int (*func_ptr)(int,int); So I would consider a function ptr type to be a unique ptr declaration syntax.

    The int (*a)[5][5]; looks more like that type of syntax. However, I would expect int *a[5][5] to make sense but it does not work as intended. Additionally, if I remove the * and do (a)[5][5]; for the declaration and then remove the * from the assignment of 1337 and the * from the printf, this also works fine.

    Finally, from what I understand, int (*a)[5][5]; creates a pointer to an array of 25 ints or 25*sizeof(int) (which is probably 100 bytes in many cases). Where is this block? Is it a stack block? Because we don't call malloc here. Yet another point of confusion for me is that in any case, we're allocating the same amount of memory between the malloc call and the array declaration... Is there any way to use a cast to tell C I want to index this "non 2 dimensional heap memory" to be indexed as a 2 dimensional array?

    Are there any other "unique pointer" types and is this some universal syntax under which function pointers and array pointers both fit or are these just 2 "special cases" where () are used? Thanks.
    Last edited by Asymptotic; 06-07-2019 at 01:13 PM.
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A "Simple" Array/String Problem. "Help" ASAP needed
    By AirulFmy in forum C Programming
    Replies: 10
    Last Post: 08-19-2015, 04:30 AM
  2. Replies: 39
    Last Post: 07-28-2013, 08:24 PM
  3. Replies: 2
    Last Post: 11-16-2011, 05:55 AM
  4. Replies: 2
    Last Post: 05-10-2005, 07:15 AM
  5. Replies: 9
    Last Post: 12-02-2003, 02:24 PM

Tags for this Thread