Bugs in a code snippet [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Spot all bugs in the code snippet below
uint arr[100]
for (uint i=99; i >=0; i--)
arr[i] = 0;
This is a question for the test, can anybody point me to all bugs in this snippet

uint isn't a type.
The first line is missing a semicolon.
i >= 0 is always true.
arr[0U - 1] is undefined behavior because it access outside the bounds of the arr array.
It's not clear that this snippet is running as part of a function. If it is not, then the entire for-loop is a syntax error.
Additionally, if this class is taking place before 1999, then:
You can't declare variables in a for loop. Instead, the uint i should be declared before the loop.
This code should probably be rewritten as simply:
unsigned arr[100] = {0};

Related

Can static variable be re-initialised [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
In the main() function,
i,a and b are decalred static. Ok, fair enough.
The function is called.
All i,a and b have some value. Ok, fair enough.
The function is called again.
Now as a static variable i , a and b must retain their value.
But
How is i again intialised to 0? (Shouldnt it contain its previous value?)
Snap shot of the problem.
P.s Answer is d btw.
i is not initialized again:
void printtab()
{
static int i, a = -3, b = -6;
i = 0;
...
It is assigned a new value when the function is entered.

Where did i make a mistake in my program i think its a logical error but i cant quite find it [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have this program that I wrote, it complies but I don't think it outputs correctly. Did I make a mistake here?
Here's my program:
#include <stdio.h>
void main(void)
{
int loop_counter = -8;
int user_input = 9;
char c1 = '9';
char c2 = 43;
while(loop_counter != 21);
{
printf("%d\n", loop_counter);
printf("%d\n", loop_counter+1);
printf("%d\n", loop_counter+2);
loop_counter = loop_counter + 1;
loop_counter++;
printf("%d\n", loop_counter);
printf("%d\n", loop_counter+1);
printf("%d\n", loop_counter+2);
getchar();
}
printf("loop exit\n\n");
getchar();
}
The biggest problem is probably that you have not articulated what you are hoping the code will do or what you intended the code to do or what you think it does. I am assuming you just need some help so my attempt to do so is below.
Running this through the compiler and interpreting the error messages helps a little.
Right off the bat, the compiler doesn't like your call to main. The compiler offers a suggestion that will successfully fix this error, so just follow the advice (and don't forget to add a
return 0;
statement before you exit main.
Second warning the compiler generates is that you have a semicolon at the end of your while statement. It also tells you how to fix it. Follow the instructions and you should be able to generate an executable.
You will still, however, have problems at runtime. This gets back to what is your intent.
With errors above corrected, your loop_counter variable enters the while loop initialized to -8, increases by 1 on line 19 and again increases by 1 on line 20. A call is made to getchar() but no input is given. Also lines 7, 8, and 9 are not used by your program.
Hope this gives you some direction. :)
The biggest error is
while(loop_counter != 21);
where the trailing ; will make the loop infinite.

C Realloc Memory [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I need to memorize how much memory I allocated with realloc().
Help me
if(!array)
array=(Type*) calloc(1,sizeof(Type));
else
array=(Type*)realloc(array,(cont+1)*sizeof(Type));
array[cont].setName(....);
cont++;
It doesn't work: after firt insert, it say: Access violation
I initialized the cont = 0 in the constructor of my class and freed memory in the destructor.
See the comments added to your code:
int count=0;
if(!array)
array=(Type*) calloc(count,sizeof(Type*); // Problem:
// missing )
// use sizeof(Type)
// calling calloc with count being zero
// so you do not allocate any memory
// use 1 instead of count
array[c].setName(EditName->Text);
c++;
count++;
array=(Type*)realloc(array,count*sizeof(Type*)); // Problem:
// use sizeof(Type)
so it should look:
int count=0;
if(!array)
array=(Type*) calloc(1,sizeof(Type));
array[c].setName(EditName->Text);
c++;
count++;
array=(Type*)realloc(array,count*sizeof(Type));
The variable c must be initialized to zero before running this code
Likewise array must be nullptr before running this code
EDIT
There seem to be one more problem if you intend to run this code several times (which I assume you do).
This line:
array=(Type*)realloc(array,count*sizeof(Type));
^^^^^
Don't use count here as you always sets count to zero
The line shall be:
array=(Type*)realloc(array,c*sizeof(Type));
In general there seems to be no real use of count

Why is the result of this short C program "3 2"? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Here is the source code:
#include <stdio.h>
enum coordinate_type{ RECTANGULAR = 1,POLAR };
int main(void)
{
int RECTANGULAR;
printf("%d %d\n",RECTANGULAR,POLAR);
return 0;
}
Why is the result the following:
3 2
You are redefining what RECTANGULAR is in the main function. It gets initialized with a "random" value, in this case it is 3, but it could be anything else.
POLAR keps its value of 2 because of how the enum is defined.
Try redefining the RECTANGULAR variable in main to see different outputs.

Please tell me answer for this code? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have this code troubling me for a while.The expression at Line 7 is troubling me. Is it giving 0 or -1. If its 0 then answer is 2 else answer is 4.
/* How to find value of c in Line 7 expression */
#include<stdio.h>
int main()
{
int a,b,c=1;
a=b=c; // a,b,c have equal value
c=b+=a=-c; // what will be the output of this expression?
c=-c;
c=(++c)*2;
printf(“%d”,c);
return 0;
}
In C the pre-increment (decrement) and the post-increment (decrement) operators requires an L-value expression as operand. Providing an R-value or a const qualified variable results in compilation error.
An lvalue is a value that can be assigned to.
/* what is value of c */
That's easy to answer: Your question does not make any sense.
Since your code won't compile, c won't have a "value".

Resources