Discussion Locked

Any thoughts on this?

0 helpful
19 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

void main(void)
{
int i=10;

func();
printf("i = %d",i);
}

void func(void)
{
/* Your code goes here, change i to 100 */
}

// No global declarations allowed, you are allowed to add ur code only in the space provided

[b]I was thinking about declaring another local variable and prontf and exit the program[/b], but obviously, thats too easy.

]:)

19 replies

by jaqui

[pre]
int main(argc, argv)
{
int i=10;

func(i);
printf("i = %d",i);
return 0;
}

int func(i)
{
i=100;
return i;
}
[/pre]

edited for formatting

by jaqui

main always has argc and argv available.

and void main is a fatal error now, has been for a few years.

the idea being that exit codes [ the return ] will help track app crashes.

by rob mekel

and that is done to little now a day's :( building in exit codes to help with tracking. On the other hand it slows down the app's so it's a matter of opinion what is best.

Or is my C to rusty now.

Rob

by jaqui

it does slow an app down slightly.
but error handling is usually really only included until after the beta cycle. It should be a part of the app forever, but they remove it from stable releases for the speed.
checking every function for errors is supposed to be done, but often gets ignored.

by rob mekel

so it comes down on the testing and final release, but heck, if you change after final testing ... where did that bug come from ... and testing even the final release.

Well it's all human so failures are inevitable.

Rob

by stargazerr

I used void main till last year.

And i cant change any arguments in this program. Cannot add any arguments to the calling function, which is the usual way to go. Everything has to be local to the function func()

]:)

by jaqui

a pointer.

*i is a reference to the location of i.
hmm..
i is declared in main, so it's a local variable.
the pointer may not work...

nope, it doesn't.

by stargazerr

I cant use anything. the only code i can put in is local to func() and pointers wont help .. :(

by jaqui

but then I'm nuts and leave usefull error codes in the returns even on a release application.
makes it a lot easier to solve the inevitable bugs that show up in use.

I got really frustrated installing a Borland Product [ Kylix 3 ] with their generic error code 10 message. took me six months to figure out they built the damned app against a specific toolchain, so installing onto newer version of the os killed the app.
So now I have a commercial ide for linux that requires a 1990 version of linux.

by m_a_r_k

If you can't pass an argument into func() and func() can't access anything outside of it or return anything, then it's impossible to change your 'i' variable. I am assuming that the interface to func is defined by others and you must use that interface. In this case, the only way to modify your 'i' variable is to use a global variable. If you have the ability to modify the internals of func(), why do you not have the ability to have it modify a global variable?

by jaqui

Stargazerr put this topic in the wrong area, it isn't actually a virus.

but no, c and c++ will hand your function the memory address to the variable automagically when you use pointers.
This makes passing data mush simpler, since one function alters the data in the initial ram allocated, rather than copying it into another ram segment as happens when you pass the variable itself.
so *i or * i is less memory intensive than:
[pre]
include // current C++ iosys.h method

int i=10

int main(argc, char, argv)
{
func(i);
printf("i equals%d\n",i);
return 0;
}
int func(i)
{
i=100;
return i;
}
[/pre]

edited for typo

by stargazerr

But people claim that this has a solution ..

M_A_R_K ... I heard you were AWOL from TR too. And its ages since i heard from you. what have you been up to ?

]:)

by m_a_r_k

Who are the idiots that claim that func() can change the variable 'i'? If func() has no visibility to 'i', how could it possibly change it? I don't know of any computer language that allows this. It defies the laws of physics, nature and logic as well. That is one of the important uses of functions. You can isolate data from them and you will be assured that a function cannot change data that it cannot see (which is why global variables are bad).

by m_a_r_k

Been busy with other things lately. Work, life, etc. Got bored with TR but I bounce in every now and then to see if a discussion catches my interest. This programming quandary of yours needed an answer from someone wise and logical so I had to step in and put the matter to rest.

by surflover

I hadn't been around much either since I took on a new company (racemi)... don't have the time to do more than pop in every now and then to see if there's any interesting posts...

it must be a planetary thing that we both checked in at the same time :-)

by m_a_r_k

Surflover? I think I remember you. :^0 Welcome back stranger, even if it is only briefly.

This discussion is closed to new replies.