Can you help me?!?!?

This is a discussion on Can you help me?!?!? within the C Programming forums, part of the General Programming Boards category; I am a noob in C, and for that fofgive me if i am stupid.. I need a program that ...

  1. #1
    pedofil
    Guest

    Question Can you help me?!?!?

    I am a noob in C, and for that fofgive me if i am stupid..
    I need a program that will read an integer from input file, and will declare (2 * integer) variables for storing...SOmething like this:

    3 - Declare (2 * 3) variables

    5 - Declare (2 * 5) variables

    Can somebody help....
    Thanks.

  2. #2
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Read the integer(x) from the file and then do

    Code:
    int *ptr=new int [2x];

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    1. Do you know how to do file i/o?
    2. Do you know how to work with dynamic memory (i.e. pointers and malloc)?


    Quote Originally Posted by abk
    Read the integer(x) from the file and then do

    Code:
    int *ptr=new int [2x];
    ...and like I said, malloc since this is C and not C++, but yeah the same idea as what I've said.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Defining variables after actual "code" statements is not standard C. You have to keep all of your defines up at the top of your function, which means getting input from the user and then defining a variable is not allowed. It is allowed in C++ and possibly C99, though I'm unsure.

  5. #5
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by hk_mp5kpdw View Post

    ...and like I said, malloc since this is C and not C++, but yeah the same idea as what I've said.
    You mean what I said is not valid in C?

  6. #6
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Quote Originally Posted by abk View Post
    You mean what I said is not valid in C?
    Correct. See my post.

    I concede it may be permissable in C99, but it definitely is not in C89. C++ allows you to do this, but not standard C for the sake of portability.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    I concede it may be permissable in C99
    I do not think it is a feature of C99.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21