A few questions

This is a discussion on A few questions within the C Programming forums, part of the General Programming Boards category; I have a few questions Is bool.h ansi standard? as gcc compiler can't find it How do i malloc a ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    18

    A few questions

    I have a few questions

    Is bool.h ansi standard? as gcc compiler can't find it

    How do i malloc a matrix of [n] rows and [m] colums?
    I know I have to use two nested loops but I don't understand how to do it thoe

    fgets reads all characters into a char array until it reaches an EOF or nl char, if it reaches a nl char it stores it.
    Normaly char arrays I handle don't have nl chars in them, is this a problem?
    I would prefer if all my char arrays were consistent and didn't have nl chars in them, how would I do this?

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    please feel free to answer just one of the questions, not all....

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by Shotgun
    I have a few questions
    Is bool.h ansi standard? as gcc compiler can't find it
    Answer: No.

    How do i malloc a matrix of [n] rows and [m] colums?
    I know I have to use two nested loops but I don't understand how to do it thoe
    Example:
    Code:
    	int i;
    	int **matrix = malloc(NUMBER_OF_ROWS * sizeof(int));
    
    	for ( i = 0; i < NUMBER_OF_ROWS; ++i )
    	{
    		matrix[i] = malloc(NUMBER_OF_COLUMES * sizeof(int));
    	}
    fgets reads all characters into a char array until it reaches an EOF or nl char, if it reaches a nl char it stores it.
    Normaly char arrays I handle don't have nl chars in them, is this a problem?
    I would prefer if all my char arrays were consistent and didn't have nl chars in them, how would I do this?
    Do you treat your char arrays as strings? If so, how do you test for the end of string? If not, you could test your array for a nl (newline) and remove it. There's various ways...

    fgets:
    The fgets() function reads at most n-1 characters from stream into the buffer pointed to by s. No additional characters are read after fgets() has read and transferred a newline character to the buffer. A null character is written immediately after the last character that fgets() reads into the buffer.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > please feel free to answer just one of the questions, not all....
    Please feel free to wait for more than 20 minutes before bumping your post with your "urgency"

    > Normaly char arrays I handle don't have nl chars in them, is this a problem?
    The FAQ tells you how to use fgets, and remove the \n if it's important to you.

    > int **matrix = malloc(NUMBER_OF_ROWS * sizeof(int));
    Should be sizeof(int*)
    Better yet,
    int **matrix = malloc(NUMBER_OF_ROWS * sizeof(*matrix) );


    p = malloc ( n * sizeof *p );
    means you never have to look outside the line itself to work out whether you're doing the right thing or not.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Is bool.h ansi standard? as gcc compiler can't find it
    To get boolean variables to work in C, either use _Bool (in C99), include <stdbool.h>, or use something like this:
    Code:
    #define bool int
    #define true 1
    #define false 0
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

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