measuring the number of tuples of the int array [duplicate] - c

This question already has answers here:
Why isn't the size of an array parameter the same as within main?
(13 answers)
Array length counting anomaly
(4 answers)
Closed 8 years ago.
I use a macro to get the number of the elements of an integer array, and I could get the right result of the number of the integer array in the main function, but I got the wrong answer if I use a getData function and send the pointer of the integer array as a parameter. I want to know why I got this wrong answer. Thank you!
the code of my program as follow:
#include <stdio.h>
#define LENGTHOFINTARRAY(intArray) ((int)(sizeof(intArray)/sizeof(int)))
int main (int argc, char *argv[])
{
int a[] = {5,8,9,4,11,7,15,25,1};
int getData(int *data);
printf("%d\n", LENGTHOFINTARRAY(a));
getData(a);
return 0;
}
int getData(int *data)
{
int i = 0;
for(i; i < LENGTHOFINTARRAY(data); i++)
{
printf("%d, %d\n", LENGTHOFINTARRAY(data), data[i]);
}
return 1;
}
and the result of my program is:
9
1, 5
I use gcc as my compiler.

The type of int* data is just int* and not int[9] like in main. The size of int* is is the size of any other pointer (4 or 8 bytes usually). There is no way to get the size of an array from the pointer.
And since arrays can't be passed by value in C (unless inside a struct or something) you have to pass the length of the array.

getData() sees data as an int*. It does not know how long it is.
You cannot determine the size of an array that is passed as a pointer to a function.

As you have defined
int a[] = {5,8,9,4,11,7,15,25,1};
int getData(int *data);
printf("%d\n", LENGTHOFINTARRAY(a));
getData(a);
so when you call "getData(a)" then it means you are passing the address of the very first element as &a[0];
so inside you function getData() as
int getData(int *data)
{
int i = 0;
for(i; i < LENGTHOFINTARRAY(data); i++)
{
printf("%d, %d\n", LENGTHOFINTARRAY(data), data[i]);
}
return 1;
}
the data is just a pointer to integer & it gets the pointer or address of the a[0];
so your macro now sees data as pointer to int. which causes the result as u have gotten.

Related

the code is giving error during compilation, please explain [duplicate]

This question already has answers here:
Is there a reason why an array name is not an lvalue?
(4 answers)
Is an array name a pointer?
(8 answers)
Closed 6 years ago.
#include <stdio.h>
int main(void) {
int values[10];
int a = 10;
values = &a;
printf ("the value is = %i.\n\n", *values);
return 0;
}
This code is written just for experimenting on pointers, I have just started learning it.
My question is that if the name of the array is a pointer then why cant we copy some other variable's address into it.
The error that it gave was "assignment to expression with array type"
please explain it in simple way.
Arrays cannot be assigned to. You can store values into array elements and you can store array addresses into pointers, but arrays themselves cannot appear on the left side of an assignment operator.
You can change values to a pointer:
#include <stdio.h>
int main(void) {
int *values;
int a = 10;
values = &a;
printf ("the value is = %i.\n\n", *values);
return 0;
}
or you can store a into values[0]:
#include <stdio.h>
int main(void) {
int values[10];
int a = 10;
values[0] = a;
printf ("the value is = %i.\n\n", *values);
return 0;
}
think of arrays as parking lots:
You can store a car into a parking spot (array element)
You can write the lot number on a piece of paper (lot pointer, you can retrieve the car by giving that to the operator).
You cannot store a parking lot into another parking lot, they are not moveable.
Array designators are non-modifiable lvalues. You may not use an array designator in the left side of the assignment expression.
Thus the compiler issues an error for this statement
int values[10];
int a = 10;
values = &a;
^^^^^^^^^^

c sizeof function of an array in a function is always four bytes despite the number of array elements [duplicate]

This question already has answers here:
"sizeof" to know the size of an array doesn't work in a function in C [duplicate]
(2 answers)
Closed 6 years ago.
When I print the (size of the array/size of the the first element), I get the right answer, but when I do the same thing in the function, I get the size of the array to be four and the size of the first element of the array to be four hence the division is always one.
#include<stdio.h>
int sizer(int *);
int main()
{
int num;
printf("Please an index: ");
scanf("%d",&num);
int array[num];
int size = sizer(array); //function to calculate array length
/*answer is always 4*/
printf("%d\n", size);
/*answer is correct*/
printf("%d\n", sizeof(array) / sizeof(array[0]));
return 0;
}
/*function to calculate array length*/
int sizer(int *array)
{
return sizeof(array) / sizeof(array[0]);
}
sizeof is not a function called at runtime, even though it looks like one. It is a feature of the compiler. It looks at a data object and replaces the sizeof() expression with a constant.
int arr[10];
int size = sizeof(arr)/sizeof(int);
This works because the compielr can see how big arr is. arr is a statically sized array here. Both sizeof expressions are replaced with the appropriate values.
int arr[10];
int size = sizer(arr);
....
int sizer(int array[]) {
return (sizeof(array)/sizeof(int));
}
This doesn't work. In sizer, array looks like an array but arrays passed in as parameters are actually just pointers to the array type. So sizeof(array) is equivalent to sizeof(int *)
scanf("%d",&num);
int arr[num];
int size1 = sizeof(arr)/sizeof(int);
int size2 = sizer(arr);
....
int sizer(int array[]) {
return (sizeof(array)/sizeof(int));
}
Here, size1 works but size2 doesn't. The creation of arr is actually allocated like this:
int arr_sizeof = sizeof(int)*num;
int *arr = alloca(arr_sizeof);
Then later on, sizeof(arr) is quietly replaced with arr_sizeof. But the compiler can only do this in the same scope that arr is created, because when arr is passed to sizer it's just converted to an int * again, so the size information is not carried over. sizer fails for thet same reason, arrays in function parameters are just passed along as simple pointers.
When you pass an array to a function you are really just passing a pointer to the first element, so in the body of sizer, the parameter array is just a pointer to int. Your function correctly returns sizeof(int*)/sizeof(int) (which is 2 rather than 4 on my machine), though this is probably not what you want. There really isn't any way for a function in C to compute the length of a passed array, which is why it is standard in C to pass the number of elements in an array as one of the parameters in any function which takes an array parameter.

How to know size of an array after passing it to a function [duplicate]

This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 9 years ago.
I have to know size of an array after passing it to a function. For example,
#include<stdio.h>
void func(char *ptr)
{
printf("%d\n",------); //Here i want the actual size of the array passed to ptr. What to fill in the blank to get total size of the arr[]
}
main()
{
char arr[10] = "Hello";
printf("%d\n",sizeof(arr)); // here it is giving 10 bytes that is equal to total size of the array
func(arr);
}
No you can't, the compiler doesn't know that the pointer at the function is pointing to an array, there are some solutions though, I can state:
1) pass the length with the function parameters :
void func(char *ptr, int length)
{
printf("%d\n",length);
}
2) if your array is always of type char, you can put a NULL char ('\0') and the use strlen
#include <stdio.h>
#include <string.h>
void func(char *ptr)
{
printf("%zu\n",strlen(ptr) + 1);
}
int main()
{
char arr[10] = {10,2,11,223,4,45,57,11, 12 ,'\0'};
printf("%zu\n",sizeof(arr));
func(arr);
}
// prints
//10
//10
Cheers
No way. You have to pass the size of array too. When you are passing an array to a function then actually you are passing pointer to its first element. In this case size of array is not known to the function.
Arrays decays into pointers when you pass to a function. With pointer alone, you can not get the size of the array. You have to pass one more argument to the calling function which is the size of the array.
Example:
#include <stdio.h>
void fun(int myArray[10])
{
int i = sizeof(myArray);
printf("Size of myArray = %d\n", i);
}
int main(void)
{
// Initialize all elements of myArray to 0
int myArray[10] = {0};
fun(myArray);
getch();
return 0;
}
Here the output will be 4 (depending on the sizeof pointer on your platform, it may vary)
This is because, “arrays decays into pointers” the compiler pretends that the array parameter was declared as a pointer and hence the size of pointer is printed.
So, you have to pass the size as one more parameter to the calling function...
#include <stdio.h>
void fun(int myArray[10], int size)
{
printf("Size of myArray = %d\n", size);
}
int main(void)
{
// Initialize all elements of myArray to 0
int myArray[10] = {0};
fun(myArray, sizeof(myArray));
getchar(); ^----------------Here you are passing the size
return 0;
}
So, here the output is 40...

Count the number of elements in an array in C [duplicate]

This question already has answers here:
size of array in c
(6 answers)
Closed 9 years ago.
How can I obtain the number of elements present in an integer array in C after the array is passed to a function? The following code doesn't work.
size=sizeof(array)/sizeof(array[0]);
In C, you can only get the size of statically allocated arrays, i.e.
int array[10];
size = sizeof(array) / sizeof(int);
would give 10.
If your array is declared or passed as int* array, there is no way to determine its size, given this pointer only.
You are most likely doing this inside the function to which you pass the array.
The array decays as pointer to first element So You cannot do so inside the called function.
Perform this calculation before calling the function and pass the size as an function argument.
You are going about it in the wrong way. I'll try to explain using a small code example. The explanation is in the code comments:
int array[100];
int size = sizeof(array) / sizeof(array[0]); // size = 100, but we don't know how many has been assigned a value
// When an array is passed as a parameter it is always passed as a pointer.
// it doesn't matter if the parameter is declared as an array or as a pointer.
int getArraySize(int arr[100]) { // This is the same as int getArraySize(int *arr) {
return sizeof(arr) / sizeof(arr[0]); // This will always return 1
}
As you can see from the code above you shouldn't use sizeof to find how many elements there are in an array. The correct way to do it is to have one (or two) variables to keep track of the size.
const int MAXSIZE 100;
int array[MAXSIZE];
int size = 0; // In the beginning the array is empty.
addValue(array, &size, 32); // Add the value 32 to the array
// size is now 1.
void addValue(int *arr, int *size, int value) {
if (size < MAXSIZE) {
arr[*size] = value;
++*size;
} else {
// Error! arr is already full!
}
}

incompatible pointer type

I just started programming so pointers and arrays confuse me.
This program just assigns random numbers from 0 - 9 into array and prints them out
(#include <stdio.h> #include <stdlib.h> #include <time.h>)
int function(int *num[]){
int i;
for(i=0; i<10; i++){
srand((unsigned)time(NULL));
*num[i] = rand()%10;
printf("%d", *num[i]);
}
return 0;
}
int main(){
int num[10];
function(&num); // incompatable pointer type (how do i fix this?)
return 0;
}
Thankyou
Change your code to read something like this:
int function(int num[]){
int i;
for(i=0; i<10; i++){
srand((unsigned)time(NULL));
num[i] = rand()%10;
printf("%d", num[i]);
}
return 0;
}
int main(){
int num[10];
function(num);
return 0;
}
In main(), you are allocating an array of 10 integers. The call to function(num) passes the address of this array (technically, the address of the first element of the array) to the function() function. In a parameter declaration, int num[] is almost exactly equivalent to int *num (I'll let you ponder on that one). Finally, when you access the elements of num[] from within the function, you don't need any extra *. By using num[i], you are accessing the i'th element of the array pointed to by num.
It may help to remember that in C, the following are exactly the same:
num[i]
*(num+i)
You don't need to pass in a pointer to your array. Just pass in the array. I have fixed your pointer code below.
Also, you should not reset the seed (srand) through every iteration through the for loop.
int function(int num[]){
int i;
srand((unsigned(time(NULL));
for(i=0; i<10; i++){
num[i] = rand()%10;
printf("%d", num[i]);
}
return 0;
}
int main(){
int num[10];
function(num);
return 0;
}
It all depends on what you want to achieve in the end.
(1) If your function function is intended to be used with arrays of run-time size, then you have to pass a pointer to the first element of the array. That is achieved by the following equivalent declarations
int function(int *num)
or
int function(int num[])
Inside the function you have to access the passed array as
num[i] = rand() % 10;
And you call your function as
function(num);
Of course, when the array size is a run-time value, it makes sense to pass that size to the function as well, meaning that your function should have the following interface
int function(int num[], size_t n)
And implement it for an array of size n, instead of hardcoding 10 directly into your implementation.
(2) If your function function is intended to be used with arrays of fixed compile-time size (10 in this case), then the better approach would be to pass a pointer to the entire array. It is achieved by the following declaration
int function(int (*num)[10])
Inside the function you have to access the passed array as
(*num)[i] = rand() % 10;
And you call your function as
function(&num);
So it is either (1) or (2).
What you currently have in your code looks like a mix of these two approaches. More precisely, you code looks like an attempt to implement the second approach, but it is missing some important parentheses :)
Most answers given to you so far suggest using the first approach. However, seeing that the array size is actually a compile-time constant in your case, I would suggest sticking with the second approach.
Use either int *num or int num[], don't use both together as you have right now (int *num[]). An array is passed to a function as a pointer to the first element, i.e. int *num, which is equivalent to int num[].

Resources