[C] Getting the pointer array size via /r/learnprogramming


[C] Getting the pointer array size

I've been playing around with pointers lately and tried to create a function that could increment through the entire array and write the value.

This is my attempt:

void write_array_content(int *pointer) { int *localPointer = (int*)pointer; register int index = 0; while (*localPointer != NULL) { printf("Value %d\t%d\n", index, *localPointer); index++; localPointer++; } } 

I've tested it on a malloc-ed array the size of 50 integers. However the final index has shown numbers like 53, 156 and 466.

As always any help would be greatly appreciated.

Submitted July 17, 2017 at 05:29PM by jansre16
via reddit http://ift.tt/2vbcx8w

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s