0
psapikas's Avatar
Light Poster
The compiler i use is LCC-WIN 32
This is how i have written the program and the warnings.

1 #include<stdio.h>
2 void afunction(int *x)
3 {
4 x=new int;
5 *x=12;
6 }
7 int main()
8 {
9 int v=10;
10 afunction(&v);
11 printf("%d",v);
12 }

Error c:\lcc\test.c: 4 undeclared identifier 'new'
Error c:\lcc\test.c: 4 operands of = have illegal types 'pointer to int' and 'int'
Warning c:\lcc\test.c: 4 possible usage of new before definition
Error c:\lcc\test.c: 4 Syntax error; missing semicolon before `int'
Error c:\lcc\test.c: 4 empty declaration
Compilation + link time:0.7 sec, Return code: 1


As for the other questions i haven't yet understand the following:
#10 why does the loop stop when i=0?

#17 i don't understand why the precedence affects the value of b
Thanks
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
I am not sure about the error but I can explain about the other 2.

When anything becomes zero its equal to false or else its true. In C there is nothing bool but everything is with respect to 0 and Non zero. 0 being false and Non Zero is true.

Say if you have something like if(1) will always be true.

Regarding precedence try expanding the macro on pen and paper and you will see both evaluation does not come out to be same.
0
psapikas's Avatar
Light Poster
Quote:
Originally Posted by shabbir
I am not sure about the error but I can explain about the other 2.

When anything becomes zero its equal to false or else its true. In C there is nothing bool but everything is with respect to 0 and Non zero. 0 being false and Non Zero is true.

Say if you have something like if(1) will always be true.
OK, i understand this. So I used the postfix i++ instead of ++i (in the same programme) and though i became 0 the loop never ended. Why is that?
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
I should become -1 in ++i for the expression to become 0 and if it becomes 0 then ++i becomes 1 and the same logic goes for i++ but its the increment that is done after the loop breaking condition check.
0
JamC's Avatar
Light Poster
Code:
1 #include<stdio.h>
2 void afunction(int *x)
3 {
4 x=new int;
5 *x=12;
6 }
7 int main()
8 {
9 int v=10;
10 afunction(&v);
11 printf("%d",v);
12 }

Error c:\lcc\test.c: 4 undeclared identifier 'new'
Error c:\lcc\test.c: 4 operands of = have illegal types 'pointer to int' and 'int'
Warning c:\lcc\test.c: 4 possible usage of new before definition
Error c:\lcc\test.c: 4 Syntax error; missing semicolon before `int'
Error c:\lcc\test.c: 4 empty declaration
Compilation + link time:0.7 sec, Return code: 1
You do not use new to allocate dynamic memory in C- you use new with C++..
0
seeguna's Avatar
Go4Expert Member
Try this coding in the manner...........u got answer
Code:
#include <stdio.h>
void afunction(int *x)

{
    x=new int;
    *x=12;
}
int main()
{
    int v=10;
    afunction(&v);
    printf("%d",v);
	return 0;
}

Last edited by shabbir; 4Aug2007 at 12:09.. Reason: Code block - http://www.go4expert.com/forums/misc.php?do=bbcode#code
0
psapikas's Avatar
Light Poster
Quote:
Originally Posted by seeguna
Try this coding in the manner...........u got answer

#include <stdio.h>
void afunction(int *x)

{
x=new int;
*x=12;
}
int main()
{
int v=10;
afunction(&v);
printf("%d",v);
return 0;
}
This also not working
0
seeguna's Avatar
Go4Expert Member
Run it in Microsoft Vc++ compiler ...
U will get an answer of value "10"
0
psapikas's Avatar
Light Poster
thanks
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by psapikas
thanks
It looks like you are getting the error in the new statement and so would like to know what si the file name you are compiling your code in. It should be a .cpp file and not a .c file or else your C compiler will not understand the new syntax.