Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here when the programme was debugged , The value of x is initialized to 1. Why is that so?
It should be initialized to 5 right?

Here when it was debugged , the variable d is initialized to 4. But , it should be initialized to 20 right?

What I have tried:

C++
#include<stdio.h>
int* findmid(int [],int);
int main()
{
    int n,i,d;
    int *c;
    int a[n];
    printf("Enter the valu of n :");
    scanf("%d",&n);
    int x;
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
    d=sizeof(a);
    x=(sizeof(a)/sizeof(a[0]));  // problem is there in this only , what is it?
    c=findmid(a,x);
    printf("%d",*c);
    return 0;
}

int* findmid(int a[],int x)
{
    return &a[x/2];
}                              
Posted
Updated 10-May-21 1:48am

Because you are using it incorrectly (your program could even crash).
Namely n is uninitialised when needed for array allocation.
You do need to first give a value to n and then (stack-)allocate the array.
Try, for instance:
C
#include <stdio.h>

int main()
{
    int n;
    printf("Enter the valu of n :");
      scanf("%d",&n);
    int a[n];
    size_t x;
    for(int i=0;i<n;i++)
      scanf("%d",&a[i]);
    x= sizeof(a)/ sizeof(a[0]);
    printf("x = %lu\n", x);
    return 0;
}
 
Share this answer
 
v2
Comments
_-_-_-me 10-May-21 8:00am    
Sorry , I asked this type of mistake already. You already explained very clearly.
Thank you very much for your valuable time.
No, it's the same problem you met a few days ago:
C
int n,i,d;
int *c;
int a[n];
You do not specify a value for n before you create the array. And you've already seen that that creates an array with zero elements, even if you change n later.
So when you use them:
for(i=0;i<n;i++)
scanf("%d",&a[i]);
You overwrite other items in your stack.

Additionally, the name of an array is a pointer to the first element - that's part fop the C specification - so
C
d=sizeof(a);
is getting the size of a pointer - 4 for a 32 bits system, 8 for a 64 bit system. It does not, will not, anc cannot return the number of elements in the array, or the total size taken by the array in bytes.
There is no function in C to do that.
So this code:
x=(sizeof(a)/sizeof(a[0]));
Is pretty meaningless - it divides the size of a pointer by the size of an integer - which will probably be 1 or 2 depending on the size of your integers (probably 32 bit so sizeof will give you 4 for four bytes) and the size of a pointer in your system (which is probably 32 bits, so 4 for four bytes since you get "1" as a result of the division).
 
Share this answer
 
Comments
CPallini 10-May-21 7:52am    
Your last point is wrong.
Since C99, sizeof is evaluated at runtime for VLA arrays, see
https://en.cppreference.com/w/c/language/sizeof
So, sizeof(a)/sizeof(a[0]) returns correctly the size of the array.
_-_-_-me 10-May-21 7:59am    
Sorry , I asked this type of mistake already. You already explained very clearly.
Thank you very much for your valuable time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900