Thread: int char []

  1. #1
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    int char []

    what is teh difference between int line[10];
    and char line[10]; I know they are arrays but when do I use int and char?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I prefer to use int for numeric values and char for characters. For example, if my array were to hold this set:

    { 1, 2, 15, 87, 45, 18, 32 }

    I would use int for my array even though char can hold each of those values adequately. If, however, my array were to hold this set:

    { 'a', 't', 'g', 'f', 'p', 'r' }

    I would use a char array even though each of those values could be held by an int and I'm not terminating the array with '\0' to make a string.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    and what if I have a mix of digits and char....

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >and what if I have a mix of digits and char....
    If the digits cannot be represented as character literals then you have a bad design:

    { 123, 'b', 34, 'Q' } /* Yucky */

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    I meant what I f I want to scan a line from text and put it in an array. That line can have anything....

    Or would I then not use an array and use something else like malloc

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I meant what I f I want to scan a line from text and put it in an array.
    This is a common operation, read the line as a character string and then parse it with either sscanf or other string conversion functions like strtol and strtod.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    what does parse it mean?

    By the way thanks for the clarification. I really appreciated

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >what does parse it mean?
    It basically means that you break the string up into independent and manageable pieces that you can then convert however you need. A simple example would be if the lines were always like this:

    int int double string

    You could parse the string like so:
    Code:
    int one, two;
    double three;
    char four[SIZE];
    char s[SIZE];
    
    fgets ( s, sizeof s, stdin );
    sscanf ( s, "%d%d%lf%s", one, two, three, four );
    Granted, this is a rather ugly and contrived example, but you get the idea.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  5. Can't Find Conio.h?
    By drdroid in forum C++ Programming
    Replies: 27
    Last Post: 09-26-2002, 04:24 AM