Array size and Pointers [duplicate] - arrays

This question already has answers here:
In C, are arrays pointers or used as pointers?
(6 answers)
Is an array name a pointer?
(8 answers)
Are empty arrays no longer allowed by gcc?
(4 answers)
Closed 2 years ago.
#include<stdio.h>
int main()
{
setbuf(stdout,NULL);
int a[],i,lim,sum=0;
printf("Enter the limit of the array: ");
scanf("%d",&lim);
printf("Enter the values: ");
for(i=0;i<lim;i++)
{
scanf("%d",&a[i]);
}
int *p;
for(p=&a[0];p<lim;p++)
{
sum=sum+*p;
}
printf("Sum= %d",sum);
return 0;
}
While running the code I'm getting the following error
..\src\Test6.c: In function 'main':
..\src\Test6.c:5:6: error: array size missing in 'a'
..\src\Test6.c:14:15: warning: comparison between pointer and integer
Please help me understand why I've to declare the array size when I have no issues doing the same without pointers.
Or please help me understand what change should I make to rectify the error:)

declare the size too when declaring the array.
eg.
int a[100000];
here you can put atmost 100000 elements in the array.

your loop
for(p=&a[0];p<lim;p++)
{
sum=sum+*p;
}
should be written as
for (p = &a[0]; p < &a[lim]; p++)
{
sum += *p;
}
or, more usually using index,
for (int index = 0; index < limit; index++)
{
sum += a[index];
}

Related

Returning the 2D array from function in C11 [duplicate]

This question already has answers here:
How to return matrix (2D array) from function? (C)
(3 answers)
Return a 2d array from a function
(3 answers)
Closed 5 months ago.
I am trying to pass the input array to the main() function, but the array is still empty after executing the enter_array() function.
I get the message "Process finished with exit code -1073741819 (0xC0000005)"
Could someone please explain to me, what should I correct in order to be able to read this newly created array in main()?
Here's my code:
#include <stdio.h>
int** enter_array(){
int a[2][2];
int row_id = 0;
int element_id = 0;
while (row_id<2){
while (element_id<2){
printf("Element number %d in row number %d:",element_id,row_id);
scanf("%d", &a[row_id][element_id]);
element_id++;
}
row_id++;
element_id = 0;}
return a;
}
int main() {
int **a;
a = enter_array();
int row_id = 0;
int element_id = 0;
while (row_id<2){
while (element_id<2){
printf("%d", a[row_id][element_id]);
element_id++;}
row_id++;
element_id = 0;
printf("\n");}
return 0;
}

counting the total element of a dynamic array [duplicate]

This question already has answers here:
Newbie questions about malloc and sizeof
(8 answers)
Closed 2 years ago.
I want to count the total element of a dynamic array.
I had search it on another source, and all I got just a static array.
How I can count it from a dynamic array?
Here is my code :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *arr;
int sz, input, length;
printf("input size of array:");
scanf("%d", &sz);
arr = (int *)malloc(sz * sizeof(int));
for (int i = 0; i < sz; i++)
{
scanf("%d", &input);
}
length = sizeof(arr) / sizeof(arr)[0];
printf("Number of elements present in given array: %d", length);
return 0;
}
and the output i just get 1
PS D:\Materi Kuliah\1031101 DASPRO\Semester 1\Week-14\Review W6-W7> ./a
input size of array:6
1
1
2
2
3
3
Number of elements present in given array: 1
arr is a pointer type, sizeof(arr) is the size of a pointer on your system. To count the number of elements, you can either use the sz variable, or if you are using GCC, use one of the builtin functions for counting the number of elements.

assignment makes integer from pointer without a cast c[a0][4]="YES"; i cant get it what is wrong int it integer t is already decleared [duplicate]

This question already has answers here:
assignment makes integer from pointer without a cast
(2 answers)
Closed 6 years ago.
warning: assignment makes integer from pointer without a cast
c[a0][4]="YES"; i cant get it what is wrong int it integer t is already decleared
char c[t][4];
for(int a0 = 0; a0 < t; a0++)
{
int n;
int k;
scanf("%d %d",&n,&k);
int a[n];
for(int a_i = 0; a_i < n; a_i++)
{
scanf("%d",&a[a_i]);
if(a[a_i]<=0)
{
count++;
}
}
if(count>=k)
c[a0][4]="NO";
else
c[a0][4]="YES";
count=0;
}
for(int p=0;p<t;p++)
printf("%c \n",c[p][4]);
c is an array of array of char. You can't set a char equal to a string.

Strange result getting length of array [duplicate]

This question already has answers here:
Why does a C-Array have a wrong sizeof() value when it's passed to a function? [duplicate]
(6 answers)
Closed 9 years ago.
Why does this return a length of 8??
#include <stdio.h>
int getLength(char arr[]) {
return sizeof(arr) / sizeof(arr[0]);
}
char text[] = "1234567890123456789";
int main (void) {
int i;
int e=getLength(text);
printf("%d\n",e);
for (i = 0; i < e; i++) {
printf("%c\n", text[i]);
}
return 0;
}
because when you pass an array as an argument, it decays to a pointer. So sizeof(arr) yields the size of the pointer (which is 8 bytes on your architecture), not the whole size of the array.
Because sizeof(array) will give you the size of the array pointer and NOT the actual size of the array itself.

C programming confused with output [duplicate]

This question already has answers here:
Weird behavior when printing array in C?
(5 answers)
Closed 9 years ago.
Can anyone please explain the output for the following program? I get an infinite loop if used a[i] = 0; and a segfault when I used a[i] = i; and also the i ranges between 0 - 9 when used a[i] = 0; whereas it goes to 39 when used a[i] = i; before giving a segfault.
#include<stdio.h>
#include<stdlib.h>
int mult(int a, int b);
int main()
{
int a[10];
int i = 0;
for(i=0; i < sizeof(a); i++)
{
a[i] = i;
printf("a[i]=%d i=%d\n", a[i], i);
}
return 0;
}
When you apply the sizeof operator to an array type, the result is the total number of bytes in the array, i.e, sizeof(a) determines the number of bytes in a which is not the number of elements in the array in this case. Use sizeof(a)/sizeof(a[0]) to get number of elements in the array a.
Replace
for(i=0;i<sizeof(a);i++)
with
for(i=0;i<sizeof(a)/sizeof(a[0]);i++)
Also, no need to initialize i twice.
You probably want to change this line:
for(i=0;i<sizeof(a);i++)
to this:
for(i=0;i<sizeof(a)/sizeof(a[0]);i++)
Note:
sizeof(a) gives the number of bytes in a[].
sizeof(a)/sizeof(a[0]) gives the number of elements in a[].
sizeof doesn't do what you think it does. It's returning the number of bytes occupied by the entire array.
You want the numeric length of the array, not the byte size.
Try something like this:
const int array_size = 10;
int a[array_size];
for (int i = 0; i < array_size; i++) {
a[i] = i;
printf("a[%d] = %d\n", i, a[i])
}
If you want to know how far to loop, without storing it in a separate const, use sizeof(a)/sizeof(a[0])

Resources