How do i find these lengths of this integer arrays? [duplicate] - arrays

This question already has answers here:
C sizeof a passed array [duplicate]
(7 answers)
Closed 1 year ago.
I want the output to be 6 and 4 four array A and B but I cant figure out where I made a mistake. I read a tutorial on StackOverflow where u can calculate a int array length by doing length = sizeof(array)/sizeof(array[0]), but it just doesn't work in my program. The values of sizeof(array) and sizeof(array[0]) stay constant regardless if I change the array as shown below.
#include<stdio.h>
#include<string.h>
int arraychecker(int array[])
{
int Length =sizeof(array)/sizeof(array[0]);
int arraylength = sizeof(array);
int arraylength0 = sizeof(array[0]);
printf("%d,%d,%d\n", Length, arraylength, arraylength0);
}
int main()
{
int a[]= {2,1,3,4,9,33};
int b[]={2,55,3,2};
arraychecker(a);
arraychecker(b);
return(0);
}
output: 2,8,4
2,8,4

Adding to Eugene's comment, some common practice is to also store the length in the array.

Related

Why does memset print different value when it's set to non zero? [duplicate]

This question already has answers here:
Memset enum array values not setting correctly (C/C++)
(2 answers)
Closed 12 months ago.
I am trying to understand memset and pointer to an array. Below is my program and when I set the array contents 0 the value of *(p+6) prints 0.
But when I set the value to 5 *(p+6) it prints 84215045
Not sure whats going on.
#include <stdio.h>
#include <string.h>
int *p;
int dtk[0xA0];
int main()
{
memset (dtk, 0, 160*sizeof(dtk[0]));
p = dtk;
printf("dtk,%d",*(p+6));
return 0;
}
As Raymond Chen alluded, each byte in the 32-bit int is being set to 5, with the result being (5<<24) + (5<<16) + (5<<8) + 5 == 84215045.

Why does the first instruction print 24 and the next one print 8? [duplicate]

This question already has answers here:
Why sizeof(array) and sizeof(&array[0]) gives different results?
(8 answers)
Closed 3 years ago.
Here is the code:
#include <stdio.h>
int main()
{
int arr[] = {10, 20, 30, 40, 50, 60};
int *ptr = arr;
printf("Size of arr[] %d\n", sizeof(arr));
printf("Size of ptr %d", sizeof(ptr));
return 0;
}
What is the difference between the two printf statements?
Take a look at the wikipedia article on how sizeof works on arrays. Essentially, it is returning the bytes required to store the entire array. In this case you have 6 ints, so this turns into sizeof(int) * 6 = 4 * 6 = 24
However, the second sizeof is getting the size of an int pointer. Your 64-bit machine has 64/8 = 8 byte pointers. Note that while pointers and arrays are usually considered the "same" in C, this is one of the areas where the behavior differs.

Reduction in size of array when passed to a function [duplicate]

This question already has answers here:
Finding length of array inside a function [duplicate]
(7 answers)
Length of array in function argument
(9 answers)
Closed 4 years ago.
This code takes input from user and simply stores them in an array.The array is then passed to a function and then printed out.The problem is that when I check the size of array in main() and then in function() they are different.Please check the output image link I provided to clearly understand what I am saying. The size of int is 4 bytes. Dev-C++ is IDE used which uses TDM-GCC 4.9.2 64 bit compiler.Can someone clarify what is going on here?
#include<stdio.h>
int function(int formal[]);
main()
{
int input[10],loop,limit;
printf("Enter limit:");
scanf("%d",&limit);
for(loop=0;loop<limit;loop++)
{
scanf("%d",&input[loop]);
}
printf("Size of input array:%d\t",sizeof(input));
function(input);
}
int function(int formal[])
{
int loop;
printf("Size of parameter array:%d\n",sizeof(formal));
for(loop=0;loop<5;loop++)
{
printf("%d\n",formal[loop]);
}
}
output: [1]: https://i.stack.imgur.com/LqHiS.png

What happens when we convert string literal to integer in C? [duplicate]

This question already has answers here:
Multiple characters in a character constant
(3 answers)
Closed 6 years ago.
Consider a C program:
#include <stdio.h>
int main (void)
{
int x = 'a';
printf("%d", x);
}
Here the output is 97 as per the ASCII value table.
But in the example below:
#include <stdio.h>
int main(void)
{
int x ='aa';
printf("%d", x);
}
The output is 24929.
Can anyone please explain how the literal has been converted to this integer value?
int x ='aa';
This is valid but value of x is implementation defined. And btw, this is not a string literal. String literal would be this "aa".
You assigned a value to the int using octets : 'a' is 0x61.
So writing int x = 'aa' is like writing int x = 0x6161.
Edit: but do not write that. Just write int x = 0x6161 or int x = 24929.

Program doesn't show expected output [duplicate]

This question already has answers here:
Printing array elements
(6 answers)
Closed 9 years ago.
The expected output of the following C program is to print the elements in the array. But when actually run, it doesn't do so.
#include<stdio.h>
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};
int main()
{
int d;
for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);//printing the array
return 0;
}//looks simple but no result
What's going wrong? Why am I not getting any output?
In the comparison
d <= (TOTAL_ELEMENTS-2)
TOTAL_ELEMENTS has type size_t so d is converted to unsigned. For, say, sizeof(size_t)==4, this makes the test
0xffffffff < 5
which fails, causing the loop to exit.
If you really want to start your loop counter from -1
d <= (int)(TOTAL_ELEMENTS-2)
would work

Resources