General discussion

  • Creator
    Topic
  • #2258728

    Any thoughts on this?

    Locked

    by stargazerr ·

    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.

    ]:)

All Comments

  • Author
    Replies
    • #3215662

      bad sg, bad sg, void func should never be used

      by jaqui ·

      In reply to Any thoughts on this?

      [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

      • #3215660

        My C is a bit rusted

        by stargazerr ·

        In reply to bad sg, bad sg, void func should never be used

        but how can you use an argument in the function if none has been specified?

        ]:)

        • #3215659

          so I missed the i in calling func()..

          by jaqui ·

          In reply to My C is a bit rusted

          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.

        • #3215654

          help track app crashes

          by rob mekel ·

          In reply to so I missed the i in calling func()..

          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

        • #3215647

          yup

          by jaqui ·

          In reply to help track app crashes

          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.

        • #3215643

          right

          by rob mekel ·

          In reply to yup

          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

        • #3215639

          I agree,

          by jaqui ·

          In reply to right

          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.

        • #3215653

          Has it?

          by stargazerr ·

          In reply to so I missed the i in calling func()..

          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()

          ]:)

        • #3215642

          then use

          by jaqui ·

          In reply to Has it?

          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.

        • #3215627

          thats the point

          by stargazerr ·

          In reply to then use

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

        • #3214384

          Then it’s impossible

          by m_a_r_k ·

          In reply to thats the point

          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?

        • #3214224

          I Know :(

          by stargazerr ·

          In reply to thats the point

          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 ?

          ]:)

        • #3214090

          Those people are wrong

          by m_a_r_k ·

          In reply to thats the point

          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).

        • #3214081

          I’m still around

          by m_a_r_k ·

          In reply to thats the point

          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.

        • #3213063

          Stargazzer and Mark, I can’t

          by old guy ·

          In reply to thats the point

          help you with your quandry on the code but I did want to say hey to Mark. We have missed you. I’m glad both you guys are back in, even if just a short time.

          Mark, you asked above where these id10ts come from, check out the answer fresh from this morning. http://techrepublic.com.com/5208-6230-0.html?PromoFeature=discussion&PromoByPassed=1&forumID=8&threadID=198959

        • #3215425

          Well M_A_R_K

          by surflover ·

          In reply to thats the point

          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 🙂

        • #3215308

          Holy cow!! Blast from the past!

          by m_a_r_k ·

          In reply to thats the point

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

        • #3214320

          dns

          by azerivista.com ·

          In reply to then use

          Does it have to point right to it= like the name servers do?

        • #3214312

          hmm?

          by jaqui ·

          In reply to dns

          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

Viewing 0 reply threads