joasdf
joasdf

Reputation: 87

Possible to run through a c-array inside a function with only one pointer as parameter?

I'm playing and learning a little with C, created an array and passed it to a function together with its size so I can run through the array and print all its elements (so I gave the function two parameters: the array itself and its size).

But now I like to do all that just by passing one parameter to the function. I got it working a little by using a pointer but I don't know how to stop because I don't have any information about arrays length, it only works in the code below because I put the array length inside the for loop. But how would that work in general if I didn't know the size and only passed one parameter to the function?

I thought it might somehow be possible to realize if a pointer points outside of the array I'm currently working with, but is that even doable? :S

void printArray(int *p){
    for(int i=0; i<4; i++){
        printf("%d ", *(p+i));
    }
}

int main(){
    int myArray[4] = {8,4,1,1};
    int *p = myArray;
    printArray(p);
    return 0;
}

Upvotes: 3

Views: 89

Answers (2)

Petr Vytovtov
Petr Vytovtov

Reputation: 167

In addition to other mentioned approaches I can offer other two:

1) You can pass the length of array as the first element (like works some containers in Pascal):

#include <stdio.h>

void print_array(int *arr)
{
    int length = arr[0];
    for (int index = 1; index <= length; ++index)
        printf("%d ", arr[index]);
    printf("\n");
}

int main()
{
    int length = 10;
    int *arr = malloc(sizeof(int) * length);
    arr[0] = length;
    for (int index = 1; index <= length; ++index)
        arr[index] = index * index * index;
    print_array(arr);
    free(arr);
    return 0;
}

2) You can create a struct for your array (like is is done for std::vector in C++ STD with class):

#include <stdio.h>

typedef struct Array
{
    int size;
    int *data;
} Array;

void print_array(Array *arr)
{
    for (int index = 0; index < arr->size; ++index)
        printf("%d ", arr->data[index]);
    printf("\n");
}

int main()
{
    int length = 10;
    Array *arr = malloc(sizeof(Array));
    arr->data = malloc(sizeof(int) * length);
    arr->size = length;
    for (int index = 0; index < length; ++index)
        arr->data[index] = index * index * index;
    print_array(arr);
    free(arr->data);
    free(arr);
    return 0;
}

Upvotes: -1

Mark Benningfield
Mark Benningfield

Reputation: 2902

The only way to traverse a pointed-to array without a length parameter is if the array contains a distinct terminator value.

For example, a C-string is "NULL-terminated" array of char values. You can traverse a char* because you know to test for the presence of the '\0' character, which has an integer value of 0.

As it applies to the code in your question, you could use -1 as a terminator value, like so:

void printArray(int *p){
  while (*p != -1{
    printf("%d ", *p++);
  }
}

Note however, that doing this requires that there is some way to interpret a valid int value as "invalid" for your purposes.

In the main, it's much easier and simpler to just pass the length of the array to the function.

Upvotes: 3

Related Questions