Strange result getting length of array [duplicate] - c

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.

Related

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.

Array size and Pointers [duplicate]

This question already has answers here:
In C, are arrays pointers or used as pointers?
(6 answers)
Is an array name a pointer?
(8 answers)
Are empty arrays no longer allowed by gcc?
(4 answers)
Closed 2 years ago.
#include<stdio.h>
int main()
{
setbuf(stdout,NULL);
int a[],i,lim,sum=0;
printf("Enter the limit of the array: ");
scanf("%d",&lim);
printf("Enter the values: ");
for(i=0;i<lim;i++)
{
scanf("%d",&a[i]);
}
int *p;
for(p=&a[0];p<lim;p++)
{
sum=sum+*p;
}
printf("Sum= %d",sum);
return 0;
}
While running the code I'm getting the following error
..\src\Test6.c: In function 'main':
..\src\Test6.c:5:6: error: array size missing in 'a'
..\src\Test6.c:14:15: warning: comparison between pointer and integer
Please help me understand why I've to declare the array size when I have no issues doing the same without pointers.
Or please help me understand what change should I make to rectify the error:)
declare the size too when declaring the array.
eg.
int a[100000];
here you can put atmost 100000 elements in the array.
your loop
for(p=&a[0];p<lim;p++)
{
sum=sum+*p;
}
should be written as
for (p = &a[0]; p < &a[lim]; p++)
{
sum += *p;
}
or, more usually using index,
for (int index = 0; index < limit; index++)
{
sum += a[index];
}

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);

sizeof error while trying to find the size of an array [duplicate]

This question already has answers here:
Why isn't the size of an array parameter the same as within main?
(13 answers)
Closed 7 years ago.
I have made this code for a homework problem but I get warning and I don't understand why. And when I open the program it doesn't work.
This is the code:
#include <stdio.h>
#include <math.h>
int binarytodec(int array[]) {
int i, result=0, n = sizeof(array)/sizeof(array[0]);
for( i=0; i<n; i++ ) {
if(array[i]==1) {
result +=pow(2,i);
} else if(array[i]==0) {
result +=0;
}
else break;
}
return result;
}
int main() {
int b[] = {1,1,1,0,0,1,0,0,12};
binarytodec(b);
return 0;
}
The warning is:
warning: sizeof on array function parameter will return size of
'int *' instead of 'int []' [-Wsizeof-array-argument] int i, result=0, n = sizeof(array)/sizeof(array[0]);
What is the problem?
Thank you
When you pass an array to a function in C, that array argument decays to a pointer and so the call to sizeof within the function you pass it to returns the size of the pointer itself, not the size in bytes of the array you pass.

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

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.

Resources