This question already has answers here:
Initializing variable length array [duplicate]
(3 answers)
Closed 2 years ago.
I want to initialise my matrix to 0, but it does not work because the first part of the matrix is a variable.
int playerCards[playerNum][10] = {0};
I want to make so all the values in the matrix are zero. I have tried the line above, but it tells me that i cannot initialise a the array. For reference, playerNum is an integer between 2 and 6, chosen by the user.
You can use memset():
int playerCards[playerNum][10];
memset(playerCards, 0, sizeof playerCards);
sizeof works for VLAs: in this case playerCards is evaluated, which does not make a difference, but also means that the size is computed at runtime as sizeof(int) * playerNum * 10 using the value of playerNum at the time of instantiation of playerCards.
Related
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:
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.
This question already has answers here:
Fortran increase dynamic array size in function
(3 answers)
Fortran array automatically growing when adding a value
(1 answer)
How to increase array size on-the-fly in Fortran?
(3 answers)
Closed 5 years ago.
I am trying to allocate an array and want to resize the array ie., grow the array size as and when required in the program.
My sample code looks like this
program main
implicit none
integer, allocatable, dimension(:)::test1
integer i, c1, c2
c1=10
c2=5
allocate(test1(1:c1))
! I basically want to do this
!allocate(test1(c1+1:c2))
end program main
How do I do this?
This question already has answers here:
Set variable text column width in printf
(2 answers)
Closed 7 years ago.
I know that I could use printf(%Nd, foo) if N is constant and I know it
But the problem is that N is in variable and calculated in program
I could do it with combining sprintf and printf:
sprintf(formatstr, "%%%dd", N);
printf(formatstr, foo);
But is there any cleaner way?
You can put a * in place of the field width. This means the next parameter will specify the width:
printf("%0*d", size, foo);
This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 9 years ago.
I am using Ubuntu 12.04lts with the GCC compiler. This program gives the result 10. Could you anybody please describe why this program gives the result like this?
#include <stdio.h>
void main(void)
{
int arr[1] = {10};
printf("\n%d\n\n", 0[arr]);
}
arr[0] gets internally expanded to *(arr+0). Similarly 0[arr] gets expanded to *(0+arr) which points to the same thing. Hence you see 10.
In general for an array or a pointer a, a[b] always means *(a+b) where a is the starting address of the array or pointer and b is the offset. Thus, a[b] and b[a] are equivalent.
below line means arr is int type array and it has size 1 and it is initialized with 10 ie index 0 has 10
int arr[1] = {10};
then next line printf statement printing the value of arr at index 0.
printf("\n%d\n\n",0[arr]);