Question

C is a reasonably new language to me, most of my programming knowledge is based around Java, or web-based languages - so please be gentle if I come across as a complete noob with this question!

I have an array of type unsigned long, with size 100000, declared in main(). When a certain condition of user input is met, a record() function call is made which initiates some hardware to begin audio recording (not really important to the scope of the question).

At the record() function call, as long as a 'ready' flag in a register is initialised, the contents of the register is copied to an array cell - this process iterates until all 100000 cells of the array have been recorded to.

The array from the record() function now needs be returned to a variable in main(). I have tried this by simply returning a variable of type unsigned long from the method call - but I can't seem to make this work. I have also tried this using pointers - but my inexperience with C is showing when I try this.

My code for using pointers is:

int main(void){
    ...
    unsigned long recordingOne[100000];
    unsigned long *ptrOne;
    ptrOne = &recordingOne;
    ...
    initiateRecording(ptrOne);
}

void initiateRecording(unsigned long *ptr){
    unsigned long returnOne[100000];
    for(i = 0; i<100001; i++){
        returnOne[i] = AD0DR1 //AD0DR1 corresponds to hardware register
    }
    *ptr = returnOne;
}

For this I get two warnings:

(in function main) warning: assignment from incompatible pointer type [enabled by default]
(in function initiateRecording) warning: assignment makes integer from pointer without a cast [enabled by default]

When I tried this previously without pointers, I tried passing an array as a parameter, and then returning an array. That looked something like this:

int main(void){
    ...
    unsigned long recordingOne[100000];
    ...
    recordingOne = initiateRecording();
}

unsigned long[] initiateRecording(){
    unsigned long toReturnOne[100000];
    for(i = 0; i<100001; i++){
        toReturnOne[i] = AD0DR1 //AD0DR1 corresponds to hardware register
    }
    return toReturnOne;
}

The compiler wasn't a fan of this either - I'm struggling to declare a return object of type unsigned long that is also an array.

As always, your help is very much appreciated!

Was it helpful?

Solution

Here is the best method:

int main(void)
{
    ...
    unsigned long recordingOne[100000];
    unsigned long *ptrOne; // <<<--- Don't need this
    ptrOne = &recordingOne; // <<<--- Don't need this
    ...
    initiateRecording(recordingOne); <<<--- Pass address of array directly
}

void initiateRecording(unsigned long *ptr)
{
    for(i = 0; i<100000; i++) <<<---- If i == 100000 undefined behavior, such as a segmentation fault, may occur; change 100001 to 100000
        ptr[i] = AD0DR1 // <<<--- write directly to array
}

The accessible elements in recordingOne are from [0] to [99999]

OTHER TIPS

The source of both compiler warnings is the following: when you initialize a C array like int array[100];, the symbol array is a pointer to the first element of the array. So when you write something like int *p = &array, the result is that p points not to the beginning of array, but to the location in memory that holds the pointer to the beginning of array. What you want is int *p = &array[0].

Edit: or int *p = array works just fine too, as pointed out in a comment in another answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top