Thread: Can you help me?!?!?

  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,817
    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
    28,413
    I concede it may be permissable in C99
    I do not think it is a feature of C99.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed