Thread: "can I do this" question

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    9

    "can I do this" question

    ok so under the advisement of my tutor/teacher I am writing my first pseudo code document for a project decently large for my history and my question is this, is it possible to pass a pointer to an array that stores the whole array, not just one part of it?

    an example would be

    Code:
    char my_array[<sub_1_max>][<sub_2_max>];
    char *myptr = my_array[<sub_1>][<sub_2>]
    the above code would point to the specific position defined by <sub_1> and <sub_2>, but how would I call a pointer to just my_array and actively access its members based on what part I want the code to access next?

    thanks, I hope this makes sense, if it doesnt ill write up some small program to illustrate.
    Last edited by Arcand; 08-02-2012 at 07:49 AM.

  2. #2
    Registered User
    Join Date
    Jul 2012
    Posts
    8
    Create a pointer to the beginning of the array &quot;can I do this&quot; question-verde-gif:


    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
        char x[] = "String";
        char *y = NULL;
        y = &x[0];
        puts(y);
        getchar();
        return 0;
    }


    I hope that I have helped &quot;can I do this&quot; question-smiled-gif.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    9
    lmao. that's so idiotically simple I cant believe I didnt think of it. Thanks

  4. #4
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Agreed. Just pass a pointer to the array. They can then calculate the offset they need to access any part of the array. If they want to fiddle with it non-destructively, they can make their own copy of it from the given pointer using memcpy (and so that you haven't sent tons of array data between functions, which actually wouldn't work on a lot of old or embedded compilers / architectures with small "stacks", but just a single pointer).

    You might also want to have the function pass the size of the array too, purely because at some point you'll realise that you'll need it in the function and have to put it in anyway (it's a very common thing to pass an array pointer and its size to a function, and how do you know how much to memcpy or whether you're accessing out of bounds if you aren't told the SIZE of the array too?).

    Personally, I'd be worried about those functions, over time, accessing the array out of bounds, so I'd probably create some sort of wrapper functions they can use to provide access, and discourage them accessing the array directly e.g.:

    Code:
    void function(char *array)
    {
        char first_element = get_first_element(array);
        int size = get_size_of_array(array);
        char nth_element = get_nth_element(array, n);
    }
    etc.

    and from there, depending on how much I trust the calling functions and what I'm doing with the array, I might even go to abstract out the concept of the array to a particular structure (e.g. struct Array, with appropriate functions that can act on it etc. and then just pass a struct Array to your function). But that's probably a step too far for a simple array in a program you've wrote yourself, if I'm honest.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 46
    Last Post: 08-24-2007, 04:52 PM
  2. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  3. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM