This question already has answers here:
How do I determine the size of my array in C?
(24 answers)
Closed 3 years ago.
I know this is a very basic question in C, so I apologize beforehand if this is naive: I'm just learning the basics a few days in.
int main() {
// initialize the variables
int num_whitespaces, num_other;
int num_digits[10];
printf("%lu", sizeof(num_digits));
}
This prints 40, which seems odd to me. Why wouldn't this print 10, which would be the length of the array in javascript or so.
sizeof returns the size in memory. Not the length of something.
Same for C++ and this link has the same example :) https://en.cppreference.com/w/cpp/language/sizeof
size of empty class: 1
size of pointer: 8
size of array of 10 int: 40
size of array of 10 int (2): 40
length of array of 10 int: 10
length of array of 10 int (2): 10
size of the Derived: 8
size of the Derived through Base: 4
Getting the length is pretty difficult in C: array_length in C
sizeof returns the size of an object in bytes
An int is 4 bytes. sizeof(int[10]) is 40.
Related
This question already has answers here:
Finding length of array inside a function [duplicate]
(7 answers)
How do I determine the size of my array in C?
(24 answers)
Closed 1 year ago.
I writing a function that will print out hexadecimal inside unsigned char array. But I can't get the size of the array. I tried using sizeof() but it seems to print out the size of the type which is 4.
if(raw_instr[0]==0x68){
printf("%d\n",sizeof(raw_instr));
for(int i=0; i<sizeof(raw_instr);i++){
printf("%x ",raw_instr[i]);//expect to print 68 10 3f 0 0 but it print only 68 10 3f 0
}
printf("\n");
}
This is the array that I will take in for loop.
unsigned char inst1[5] = {0x68,0x10,0x3f,0x00,0x00};
This question already has answers here:
How dangerous is it to access an array out of bounds?
(12 answers)
No out of bounds error
(7 answers)
How many members calloc allocates in C [duplicate]
(4 answers)
Closed 1 year ago.
Here I have declared an integer array of size 2 but I am able assign more than 2 elements to the array and also print them but the array size is indicated as of size 8bytes before and also after assigning a value to it.
int main()
{
int a[2];
printf("size is %d\n",sizeof(a));
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
a[4]=5;
printf("%d %d %d %d\n",a[0],a[1],a[2],a[3],a[4]);
printf("size is %d\n",sizeof(a));
}
Output
size is 8
1 2 3 4
size is 8
So is it possible to assign more elements than the specified size to an array? If yes is it only applicable if you declare the array in this fashion and can this be applied to any type of array in c?
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.
This question already has answers here:
How do I determine the size of my array in C?
(24 answers)
Closed 4 years ago.
We have been given a large array of unknown size with elements given , is there any function or something other through which we can find the size of that array in language C
int a[]={4,6,4,26,3,2,5,7,3,7,3,2,5,4,6,3,7,232,6,32,6,3,7,3,6,2,5,7,3,6,3,6,36,3,67,23,6};
The size in bytes you can get by
sizeof(a);
The number of elements in that array you can get by
sizeof(a)/sizeof(a[0]);
This question already has answers here:
Why doesn't my program crash when I write past the end of an array?
(9 answers)
How dangerous is it to access an array out of bounds?
(12 answers)
Closed 5 years ago.
Here is my code:
#include<stdio.h>
int main()
{
int i, list[1];
list[0]=1;
list[1]=2;
list[2]=7;
list[55]=70;
i=sizeof list;
printf("%d %d %d %d %d Size of array is %d",list[0],list[1],list[2],list[3],list[55],i);
return(0);
}
It returns "1 2 7 4 70 Size of array is 4". Why can i assign, say 55 to list[55]. list[55] should not exist as I only gave the array list enough memory for 1 integer, right? In addition shouldn't this give me an error as list[3] doesn't exist? and if for some reason i am changing the size of the array why isn't the size 56? It comes out as 4.
So what is happening to give me the output i got?<--{main question}
[As i don't want to create a separate thread for a related question, why when i code int list[0]; the program crashes, if i am somehow changing the size from 1 to 4 shouldn't I be able to change the size from 0 to 4?]
Thanks for your help, I know this probably a stupid or obvious question.