When I run this program-
#include <stdio.h>
void inc( int num[], int n)
{
int i;
n++;
for(i=0;i<10;i++)
num[i]++;
}
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int a=2;
inc (arr, a);
int i;
for(i=0;i<10;i++)
printf("%d ", arr[i]);
printf("\n%d ", a);
return 0;
}
I get the output-
2 3 4 5 6 7 8 9 10 1
2
I understand why the int is unchanged but I don't understand why the array is getting changed since I have not used pointers to call the array. I know that the function will make a different copy of n and assign n=a and all changes will happen to n only and a will be unchanged. Why the array is getting changed?
In C, except for a few cases, an array name decays (=gets implicitly converted) to a pointer to its first element.
This
void inc(int num[], int n)
is exactly same as this:
void inc(int *num, int n)
Related
This question already has answers here:
Why sizeof(param_array) is the size of pointer?
(8 answers)
Closed 9 months ago.
I am trying to find the smallest missing element of an array using function check, which has two arguments (n and array A). I can't understand why my function check is always returning one and the while loop is never closing.
#include <stdio.h>
bool check(int n, int A[])
{
for (int i = 0; i < sizeof(A); i++)
{
if(A[i] == n)
{
return 1;
}
}
return 0;
}
int main()
{
int A[] = {1, 3, 6, 4, 1, 2};
int n = 1;
while (check(n, A) == 1)
{
n++;
}
printf("%d is missing",n);
}
The compiler adjusts a parameter having an array type to pointer to the array element type.
So this function declaration
bool check(int n, int A[])
is equivalent to
bool check(int n, int *A );
And within the function the expression sizeof(A) is equivalent to the expression sizeof( int * ) and is equal to either 4 or 8 depending on the used system.
Thus this for loop
for (int i = 0; i < sizeof(A); i++)
invokes undefined behavior.
I know but still that's not why the while loop is never stopping.
Answering your above comment it seems that in the used system sizeof( int * ) is equal to 8 and the variable n is placed in memory after the array A as they defined in main
int A[] = {1, 3, 6, 4, 1, 2};
int n = 1;
As a result you get the infinite wile loop because in the for loop within the function the memory occupied by the variable n is checked and n is always equal to itself.
Thus the function always returns 1.
That is in the for loop the array is traversed as it has 8 elements like
int A[] = {1, 3, 6, 4, 1, 2, n, some_indeterminate_value };
I have tried for two hours and cannot find the answer.
If someone can help I will be very thankful.
#include <stdio.h>
void somefunction(const int[], int);
int main() {
int a[] = { 1, 3, 4, 5, 7, 9, 11 };
somefunction(a, 5);
return 0;
}
void somefunction(const int b[], int c) {
if (c > 0) {
somefunction(b[], c - 1);
printf("%d ", b[c]);
}
}
if (c > 0) is the problem. You need to make it
if (c >= 0) to print the value of 1 inside a[0].
Also the first argument at the recursive call of somefunction inside somefunction needs to omit the [].
As additional hint, to print the value of 11 in a[6] you need to change
somefunction(a,5);
in main() to
somefunction(a,6);
The resuming code is this:
#include <stdio.h>
void somefunction(const int[], int);
int main() {
int a[] = { 1, 3, 4, 5, 7, 9, 11 };
somefunction(a,6);
return 0;
}
void somefunction(const int b[], int c) {
if (c >= 0) {
somefunction(b, c - 1);
printf("%d ", b[c]);
}
}
Output:
1 3 4 5 7 9 11
I am new to C and am having a problem reading this matrix declaration in a function.
void foo(int (*array)[][3], int row, int col)
and in the function how do I access an array element - say to print it's value.
int (*array)[][3]
declares array to be a pointer to a 2D array whose second dimension is 3. Example usage:
#include <stdio.h>
void foo(int (*array)[][3], int row, int col)
{
printf("%d\n", (*array)[row][col]);
}
int main()
{
int array[10][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
foo(&array, 2, 2);
return 0;
}
Output:
9
(*array)[][3] is a pointer to 2D array.it can point to an int array of variable rows and 3 columns.Here is an example:
int main(void)
{
int arr[3][3] =
{
{0,0,0},
{1,0,0},
{1,1,0},
};
int (*array)[3][3],row,col;
array = &arr;
printf("arr[1][0] : %d\n",(*array)[1][0]);
}
I am trying to print out the contents of array1 times 10 if the integer inside is positive, if it is negative , the number should be left as it is in an another array. My code doesn't give me any result, I'm not that good in programming so I'm pretty sure I'm making a stupid mistake but I can't make out what it is, can someone point out the problem
void
tenfold (int array2[], int size )
{
int i, array1[size];
for (i=0;i<size;i++)
{
if (array1[i]>0)
array2[i]= 10 * array1[i];
else
array2[i]= array1[i];
}
}
int main()
{
int i,array1[9]= {3, 4, 5, 6, 7, -8, -9, 1, 2};
int size = 9;
tenfold(array1, size);
return 0;
}
tenfold should take array1 as input and array2 as local, other wise your are correct,
added a printf in tenfold to see the output.
#include<stdio.h>
void tenfold (int array1[], int size )
{
int i, array2[size];
for (i=0;i<size;i++)
{
if (array1[i]>0)
array2[i]= 10 * array1[i];
else
array2[i]= array1[i];
}
for(i=0;i<size;i++)
printf("(orignal) array1[%d]=%d, (new) array2[%d]=%d\n", i, array1[i], i, array2[i]);
}
int main()
{
int i,array1[9]= {3, 4, 5, 6, 7, -8, -9, 1, 2};
int size = 9;
tenfold(array1, size);
return 0;
}
Output:
(orignal) array1[0]=3, (new) array2[0]=30
(orignal) array1[1]=4, (new) array2[1]=40
(orignal) array1[2]=5, (new) array2[2]=50
(orignal) array1[3]=6, (new) array2[3]=60
(orignal) array1[4]=7, (new) array2[4]=70
(orignal) array1[5]=-8, (new) array2[5]=-8
(orignal) array1[6]=-9, (new) array2[6]=-9
(orignal) array1[7]=1, (new) array2[7]=10
(orignal) array1[8]=2, (new) array2[8]=20
You have changed the names of array in the function, update code to
//------------v array1
tenfold (int array1[], int size )
{
int i, array2[size]; //here array2
//your code
}
With your code, array1[] is never initialized and has random values, so if will not work as expected.
You should first start by trying to printf the values in tenfold(). :-)
You don't need to create array1 in tenfold().
Also, you may be able to get away from using a size variable and just use sizeof(array)/sizeof(array[0]).
tenfold doesn't print anything. There is no need to create another array if all you want if for that function to print. Be sure to add #include
#include <stdio.h>
void tenfold (int array[], int size)
{
int i;
for (i=0;i<size;i++)
{
if (array[i]>0)
printf("%d ", (10 * array[i]));
else
printf("%d ", array[i]);
}
}
output:
30 40 50 60 70 -8 -9 10 20
I'm trying to print all of the values in the four arrays by sending them to a separate function. But, the problem is I can't get the function to print the all of the integers in the array because I'm not sure what I could set the condition statement in the for loop to, that would be universal to any array of any size.
Right now the function only prints the first 11 numbers. I assume that's because the first number in that array is 11.
#include <stdio.h>
void print_array(int a[]);
void find_max(int b[]);
void find_min(int c[]);
void search(int d[]);
void SORT(int e[]);
int main(void)
{
int first[11] = {7,7,7,7,7,7,7,7,7,7,7};
int second[14] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2};
int third[16] = {-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int fourth[23] = {-3, 4, 33, 22, 9, -100, 2, 56, 57, 55, 2, 90, 2234, 32, 8, 123, 2, 33, 22, 22, 33, -1, -3};
print_array(&second[0]);
return(0);
}
void print_array(int a[])
{
int i;
for(i=0;i<*a;i++)
{
printf("%d ",a[i]);
}
}
Pass a second argument to your function that takes the length of the array. For example:
print_array(int *array, int length)
{
for (int i = 0; i < length; i++) { /* whatever */ }
}
The function has no way of knowing when the array ends. This piece of data simply does not exist unless you pass it manually. The array is just a sequence of bytes in the memory, it has no end delimiter. So you should add a parameter to the function telling it the length of the array.
Yep, this is how it works in C.
Change the function to:
void print_array(int a[], size_t a_size) {
int i;
for(i=0; i< a_size;i++)
// ...
And change the calling of the function to pass in the size:
print_array(second, sizeof(second)/sizeof(second[0]));
Which will calculate the memory size of the array (for a 4 int array on a 32 bit system it'll be 16) and divide it by the size of an int (on a 32 bit system, it's 4 bytes).
in C you can make it with a function and macro:
void printArray_(int *a, int len) {
for (int i = 0; i < len; i++) printf("%d ", a[i]);
}
#define printArray(arr) printArray_((arr), sizeof(arr)/sizeof(arr[0]))
int main(int argc, _TCHAR* argv[])
{
int data[] = { 1,2,3,4 };
printArray(data);
return 0;
}
output:
1 2 3 4
Change this line
print_array(&second[0]);
To
print_array(&second);
Because,
&second[0] just passes the reference to the element at 0th position,which will not be able to traverse the array.
And we cannot traverse the array passed by reference without the size.As there are arrays of varied size, we can compute the size of the array by,
int array_length = sizeof(array)/sizeof(array[0]);
Change the line
void print_array(int a[])
To
void print_array(int *a,int array_length)
And the function of array printing will be as,
void print_array(int *a,int array_length){
int i;
for(i=0;i<array_length;i++){
printf("%d ",*a);
a++; //for incrementing the position of array.
}
}