**#include <stdio.h>
#include <string.h>
void buildArray();
int main(void)
{
int example[20];
buildArray(example);
getch();
}
void buildArray(int param[]) {
int i = 0;
do {
printf("Please enter number :");
scanf("%d", param[i]);
i++;
} while (param[i] != -1);
}**
hello i want to get input from user. when will user entered -1 number , i want to break this loop. But i getting some error code : ""Unhandled exception at 0x53918F0E (ucrtbased.dll) in HelloC.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC.""
where is my fault? When i was try to change function type from void to int, getting same error.
There are few issues, firstly this
scanf("%d", param[i]);
should be
scanf("%d", ¶m[i]); /* need to provide address */
Also buildArray()
prototype should be
void buildArray(int *);
instead of
void buildArray();
Also do..while
condition is wrong, use param[i-1]
instead of param[i]
in condition part of the loop.
Sample Code
void buildArray(int param[]) {
int i = 0;
do {
printf("Please enter number :");
scanf("%d", ¶m[i]);
printf("entered : %d\n", param[i]);
i++; /* because of this, condition part should be param[i-1] != -1 */
}while(param[i-1] != -1); /* it should be param[i-1] */
}
User contributions licensed under CC BY-SA 3.0