Thread: Getting a general understanding how this stuff works.

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    11

    Getting a general understanding how this stuff works.

    So I'm trying to build up my fundamental knowledge of how this stuff works. And I have a few easy questions that would help an entry person understand how things work. I have read a few write ups and manuals, but most assume a previous knowledge of the terminology so it was harder to comprehend.

    Int and char... I know they are different data types. I know that they have different amounts of bytes allocated to them by default. But one burning question I have is about how they work. For example. If I intend on reading and writing to a file that has numbers and letters and I use the int array, what exactly is the difference? does it manipulate the letters differently? what is a good rule of thumb when it comes to deciding what data type to use?
    If I want a char array can I store a literal number in one of the elements if its a char array? What determines the array type, input or output?

    Ok, that's all for now. Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by overclocking
    If I intend on reading and writing to a file that has numbers and letters and I use the int array, what exactly is the difference? does it manipulate the letters differently?
    Why would you use an int array? For example, you might try reading into the int array using fscanf, but because the input is invalid for the type, you would get conversion failures. Or, you might try reading line by line using fgets, then parse the string using say, sscanf, but because the contents of the string is invalid for the type that you want to parse it as, you would get conversion failure.

    In other words, if you intend on reading and writing to a file that has numbers and letters and you use an int array, then you are setting yourself up for failure... unless you somehow interpret the "numbers and letters" in a way that makes sense to populate an int array.

    Quote Originally Posted by overclocking
    If I want a char array can I store a literal number in one of the elements if its a char array?
    It depends on the value of the "literal number" and the range of values for char.

    Quote Originally Posted by overclocking
    what is a good rule of thumb when it comes to deciding what data type to use?
    (...)
    What determines the array type, input or output?
    I recommend thinking about what you are trying to build, and deciding the data types depending on what you need to solve the problems that you set out to solve.

    For example, if you are writing a program to play tic-tac-toe, then you need some kind of representation for the game grid that you can manipulate, so you concentrate on that. You don't have to actually store 'x' and 'o' in the grid. Rather, when you want to print the grid, you convert this internal representation to a string that can be printed. Likewise, you don't have to constrain your internal data structures to the input expected from the user, e.g., you could label the tiles on the grid with letters A to I, yet internally you convert these letters into integers (e.g., of type size_t) corresponding to indices of an array rather than characters (of type char).
    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

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    45
    I thought I would point out that int and char aren't required to have different sizes, although in practice they almost always do.

    Your questions are actually a bit more complicated than you may have thought. Characters and integers aren't actually different kinds of data. Consider how the character constant 'a' maps to an integer value in accordance with your character set. This means that if you store 'a' in one char and you store 123 in a second char, both chars may store the exact same value.

    Take some time to understand that and I'm sure you'll get the hang of it.

  4. #4
    Registered User
    Join Date
    Feb 2014
    Posts
    11
    Quote Originally Posted by laserlight View Post
    Why would you use an int array? For example, you might try reading into the int array using fscanf, but because the input is invalid for the type, you would get conversion failures. Or, you might try reading line by line using fgets, then parse the string using say, sscanf, but because the contents of the string is invalid for the type that you want to parse it as, you would get conversion failure.

    In other words, if you intend on reading and writing to a file that has numbers and letters and you use an int array, then you are setting yourself up for failure... unless you somehow interpret the "numbers and letters" in a way that makes sense to populate an int array.


    It depends on the value of the "literal number" and the range of values for char.


    I recommend thinking about what you are trying to build, and deciding the data types depending on what you need to solve the problems that you set out to solve.

    For example, if you are writing a program to play tic-tac-toe, then you need some kind of representation for the game grid that you can manipulate, so you concentrate on that. You don't have to actually store 'x' and 'o' in the grid. Rather, when you want to print the grid, you convert this internal representation to a string that can be printed. Likewise, you don't have to constrain your internal data structures to the input expected from the user, e.g., you could label the tiles on the grid with letters A to I, yet internally you convert these letters into integers (e.g., of type size_t) corresponding to indices of an array rather than characters (of type char).
    Thanks, Its starting to make a little more sense, As I get more exposure like this, I am understanding it better.

    So is it possible to use fopen to open a file, use fgets to read the data into a char array, storing one letter/number per element, then read from the char array with printf to print one element at a time?
    If I understand what you are saying, this would not provide a conversion failure since it is not trying to convert from an int?
    Or did I totally misunderstand?

    I really appreciate the help on this forum. Its slowly all starting to come together for me. It was hard at first, as I had zero exposure to data handling before starting coding.
    Thanks

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    So, you can write anything to anywhere. The real trick is having what you write be coherent otherwise, you're just writing something the machine can't decode.

    And your idea to read from a data file is correct. Allocate enough space for the file and have the computer treat it like a character, writing to a properly aligned buffer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. general question about how struct works.
    By nullifyed in forum C Programming
    Replies: 14
    Last Post: 06-21-2010, 06:03 AM
  2. Beginner question about understanding general Syntax
    By lucidrave in forum C++ Programming
    Replies: 15
    Last Post: 08-13-2009, 03:57 PM
  3. Help understanding how "operator char*()" works.
    By Lawn Gnomusrex in forum C++ Programming
    Replies: 28
    Last Post: 10-05-2008, 01:59 PM
  4. Replies: 8
    Last Post: 09-09-2005, 12:34 AM
  5. Help understanding Headers and Other beginner stuff
    By Saintdog in forum C++ Programming
    Replies: 3
    Last Post: 12-01-2004, 09:34 PM