Question about arrays.

This is a discussion on Question about arrays. within the C Programming forums, part of the General Programming Boards category; I have a two dimensional array that will be taking in about 38 numbers and 2 letters randomly. I allocate ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    23

    Question about arrays.

    I have a two dimensional array that will be taking in about 38 numbers and 2 letters randomly.


    I allocate the memory for 20 columns and 20 lines and take in simple 1 digit numbers and two letters randomly.

    How does the array deal with the letters getting scanned in as %d or is there a special way to store 2 characters in a int array?

    Will the scanf store the letters as there acsii code?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    In reality, you can't store a character in an integer array. You can store the ascii value of a character into an integer array, buy you'd have to cast it going in and coming out(if you want it read as a character).

    If you simply did scanf("%d",&foo) where foo is a character any character value the user inputs simply wouldn't be read in.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    23
    Ok gotcha, so if I didn't know when the character was going to be coming in how would I cast it to take the acsii code for the two letters while still taking in the integers?

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    You can't. The only thing you can do is see if your printf failed. If it fails it means it didn't properly read into the variable. If it fails, you'll want to clear the file and try to read in again or try to read in a different datatype.

    Code:
    #include <stdio.h>
    
    int main() 
    {
      char foo;
      
      printf("Enter a character: ");
    
      if (scanf("%d",&foo))
         printf("scanf() succeeded");
      else
         printf("scanf() has failed");
      
      return 0;
    }
    Compile this and watch what happens. Then change the %d to %c and try again.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    23
    so should do something like

    if(fscanf("%d", &array[i][j])

    else(fscanf("%c", &array[i][j])

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    You really shouldn't use scanf() for this because it can be unsafe. This is because %d will read into characters if a decimal number was given. What it reads in is the ascii value and implicitly casts it.

    Code:
    #include <stdio.h>
    
    int main() 
    {
      char foo;
      
      scanf("%d",&foo);  // Enter the value 65 here
      printf("%c", foo);   // and get the value 'A' here
    
      return 0;
    }
    Compile this and enter 65.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about arrays.
    By Kelvie in forum C++ Programming
    Replies: 3
    Last Post: 09-17-2007, 05:32 AM
  2. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 07:24 PM
  3. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. Question about arrays?? PLease read..
    By foofoo in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 02:40 PM

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