The length of array changes after passing it a function [duplicate] - c

This question already has answers here:
Why the array size is 1 [duplicate]
(4 answers)
Closed 7 years ago.
I use the following code to pass an array to a function and print its size before and after calling the function:
#include <stdio.h>
int passing_array(int array[]) {
int size = sizeof(array);
printf("The size after passing: %d\n", size);
}
int main () {
int a[] = {1, 2, 3};
int size = sizeof(a);
printf("The size before passing: %d\n", size);
passing_array(a);
return 0;
}
After running the code, I got a strange result:
The size before passing: 12
The size after passing: 8
My question is: why does the size of the array change from 12 to 8?

int passing_array(int array[]) {
int size = sizeof(array);
printf("The size after passing: %d\n", size);
}
is the same as
int passing_array(int* array) {
int size = sizeof(array);
printf("The size after passing: %d\n", size);
}
Hence, size is the size of a pointer.

When you pass an array to a function, it decays to a pointer to the first element of the array. So int array[] in your parameter list is really int *array, and sizeof is returning the size of the pointer.

Related

Why does my A[] function argument have size 8 bytes instead of 4?

I'm learning about C Pointer from freeCodeCamp.org and I'm getting stuck at the Array pointer as the function arguments follow by this.
In the instructor code was like this
int SumOfElement(int A[]) {
int i, sum = 0;
int size = sizeof(A)/sizeof(A[0]);
printf("SOE - Size of A = %d, size of A[0] = %d", sizeof(A), sizeof(A[0]));
for(i = 0; i<size; i++)
{ sum += A[i];}
return sum;
}
int main() {
int A[]= {1,2,3,4,5};
int total = SumOfElements(A);
printf("Sum of elements %d\n",total);
printf("Main - Size of A = %d, size of A[0] %d",sizeof(A),sizeof(A[0]));
}
And his result (from interested part) is :
SOE - Size of A = 4
Main - Size of A = 20
I understood why's that. But in my code when I try to run similar code I got 8 from printing sizeof(A) inside function
int myFunc(int a[]) {
printf("Size of a[] inside function is %d\n", sizeof(a));
return a;
}
int main(){
int a[] = {1, 2, 3, 4, 5};
myFunc(a); // Result is 8
printf("Size of a[] in main function is %d\n", sizeof(a)); // Result is 20
return 0;
}
int A[] is an array declaration, but since it is part of a function parameter list, it gets implicitly adjusted into a pointer to the first element of that array.
int SumOfElement(int A[]) is 100% equivalent to int SumOfElement(int* A).
Therefore it is senseless to do sizeof A inside that function, because doing so will always give you the size of the pointer. Which is typically 4 bytes on a 32 bit system but 8 bytes on a 64 bit system.
For the same reason, int size = sizeof(A)/sizeof(A[0]); is nonsense. You can't calculate an array size like that when A is an array which has decayed into a pointer. This gives you the size of a pointer divided with the size of an int.
For this function to make sense, it should probably have a separate size parameter.

Why did the array lose the last element when passed into another function? [duplicate]

This question already has answers here:
Why isn't the size of an array parameter the same as within main?
(13 answers)
Closed 1 year ago.
I made an static int arr[3] and set to 1, 2, 3.
I can print 1, 2, 3 in the same function.
But when I pass the array it only prints 1, 2.
Why? What happened? How do I fix this?
#include <stdio.h>
void printArray(int* arr){
size_t n = sizeof(arr)/sizeof(arr[0]);
for(int i=0; i<n; i++){
printf("%d\n",arr[i]);
}
}
int* makearray()
{
static int arr[3];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
printf("The Array before passing onto stack\n");
size_t n = sizeof(arr)/sizeof(arr[0]);
for(int i=0; i<n; i++){ printf("%d\n",arr[i]);}
printf("\nThe Array when attempting to pass to another function\n");
printArray(arr);
return arr;
}
int main(void) {
int* m = makearray();
return 0;
}
By converting your array to a pointer, you removed the information that sizeof needs to know its total length.
Instead, it just gives you the size of a single int *, which is twice the size of a single int on your machine.
Hence, you only see the first two elements.
In C, you're basically forced to pass around the length alongside the pointer. You can wrap the two in a struct to make it easier to use, but don't expect the niceties of other languages you may be used to.

Realloc changes the values of the array but array still has the same address in C Programming [duplicate]

This question already has answers here:
Dynamic memory access only works inside function
(1 answer)
How do I modify a pointer that has been passed into a function in C?
(7 answers)
Closed 2 years ago.
I have a question regarding dynamic allocation.
Why do the array values change after using realloc? It works fine if the initial size of the array is greater than the next size (e.g. 10 20) but if the initial size of the array is lesser than the next size (e.g. 20 10), the code gives me garbage values.
The code works properly in local side but when I try it on online compilers, it does not work the way it's supposed to.
Is there anything wrong with my code? Or is it a problem with online compilers? And am I using the realloc function properly?
#include<stdio.h>
#include<stdlib.h>
void print(int *ptr, int size);
void reallocate(int *ptr, int size);
void assign(int *ptr, int size);
int main(void) {
int n;
// Set initial size for array
scanf("%d",&n);
int *arr = (int*) malloc(n * sizeof(int));
// Assign values from 0 to n-1
assign(arr, n);
// Print values assigned
print(arr, n);
printf("\n");
// Set new size for reallocation
scanf("%d",&n);
reallocate(arr, n);
// Assign values again
assign(arr, n);
// Print values
print(arr,n);
return 0;
}
// Prints values of the array
void print(int *ptr, int size) {
for(int i = 0; i < size; i++) printf("%d ",ptr[i]);
}
// Reallocates new size for array
void reallocate(int *ptr, int size) {
ptr = realloc(ptr, size*sizeof(int));
}
// Assigns values from 0 to n-1
void assign(int *ptr, int size) {
for(int i = 0; i < size; i++) ptr[i] = i;
}
I hope for your responses.

Returning pointer to array doesn't give expected output in c [duplicate]

This question already has answers here:
returning a local variable from function in C [duplicate]
(4 answers)
Closed 5 years ago.
#include <stdio.h>
int* createReverseArray(int *array, int size)
{
int i;
int newArray[size];
for(i=size-1; i>=0; i--)
{
newArray[i] = array[size-i-1];
}
return newArray;
}
int main(void) {
int myArray[] = {1,2,3,5,1};
int myArray2[] = {1,2,3,4,2,1};
int i;
int size = sizeof(myArray)/sizeof(int);
printf("Size: %d\n", size);
int* newArray = createReverseArray(myArray, size);
for(i=0; i<size; i++)
{
printf("%d, ", newArray[i]);
}
return 0;
}
I printed the array within the createReverseArray function and got the correct output, but when I return the pointer to the array and then try to print the results, I think it's printing pointers to each array spot? I'm not quite sure.
This returns:
Size: 5
12341002, 1, -10772231820, -1077231812, 1074400845,
newarray is an automatic local variable. It will no longer exists once function return. Returning pointer to an automatic local variable invoke undefined behaviour and in this case nothing good can be expected.
You can allocate memory diynamically and then return pointer to it
int *newArray = malloc(sizeof(int)*size);

Strange result getting length of array [duplicate]

This question already has answers here:
Why does a C-Array have a wrong sizeof() value when it's passed to a function? [duplicate]
(6 answers)
Closed 9 years ago.
Why does this return a length of 8??
#include <stdio.h>
int getLength(char arr[]) {
return sizeof(arr) / sizeof(arr[0]);
}
char text[] = "1234567890123456789";
int main (void) {
int i;
int e=getLength(text);
printf("%d\n",e);
for (i = 0; i < e; i++) {
printf("%c\n", text[i]);
}
return 0;
}
because when you pass an array as an argument, it decays to a pointer. So sizeof(arr) yields the size of the pointer (which is 8 bytes on your architecture), not the whole size of the array.
Because sizeof(array) will give you the size of the array pointer and NOT the actual size of the array itself.

Resources