Thread: help on declarations

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    76

    help on declarations

    I am working on trying to declarations and using typedef.
    this is my assignment
    Write and compile a program in C to declare data variables of each of following types: unsigned char, unsigned short, unsigned int, unsigned long int. Use Typedef. Use meaningful names for type definitions. Declare a data variable for each of the type definitions and initialize them with 255, 65535 and 4294967295 respectively. Write the comments for each declaration.

    I have used typedef to remame declarations but now after I declare a variable it will not compile.

    please the my attachment for what I wrote

    I get a compiler log error like this:

    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\Documents and Settings\dustin\My Documents\computer programming class\typedef.cpp" -o "C:\Documents and Settings\dustin\My Documents\computer programming class\typedef.exe" -traditional-cpp -g3 -I"C:\Dev-Cpp\include\c++" -I"C:\Dev-Cpp\include\c++\mingw32" -I"C:\Dev-Cpp\include\c++\backward" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3
    C:/Documents and Settings/dustin/My Documents/computer programming class/typedef.cpp: In
    function `int main()':
    C:/Documents and Settings/dustin/My Documents/computer programming class/typedef.cpp:10: parse
    error before `=' token

    C:/Documents and Settings/dustin/My Documents/computer programming class/typedef.cpp:10:11: warning: multi-character character constant

    Execution terminated

    I have no clue what I am doing wrong can someone point me in the right direction thank you.
    Last edited by robasc; 03-05-2005 at 10:00 AM.

  2. #2
    Hello,

    All variables must be declared before use, although certain declarations can be made implicitly by context. A declaration specifies a type, and contains a list of one or more variables of that type as in:
    Code:
    int a, b, c;
    char s, l;
    char m = 'a';
    If this makes sense, then you may find that:
    Code:
    char = 'h';
    Is an invalid declaration.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    I still do not understand.
    I am very new at this, so perhaps you could simplify this a little more for me to understand.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    typedef is used to create something like a synonym to the data type.
    Code:
     typedef int num1;
    then you can use num1 to declare variables as int as follows
    Code:
    num1 x, y;
    which is equivalent to
    Code:
    int x, y;
    When no one helps you out. Call google();

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    Code:
    So I could develope this
    
    typedef unsigned char u_char
    int main(){
    u_char = '255';
    }

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    With typedef you are creating a new type, like an int, float, etc.
    You can think of typedef like this:
    whenever you do
    Code:
    u_char a = 'b';
    the compiler will treat it as
    Code:
    unsigned char a = 'b';
    so u_char

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    Wow! This is very confusing

    Ok, I understand that I can use typedef to redefine unsigned char to u_char, intern this is simplifying the term unsigned char, like this
    Code:
     
    typedef unsigned char u_char;
    is this correct?

    now

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    yes, now you can use u_char to declare variables as unsigned char
    When no one helps you out. Call google();

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    Now the next thing I need to do is this

    Use meaningful names for type definitions. Declare a data variable for each of the type definitions and initialize them with 255, 65535 and 4294967295 respectively. Write the comments for each declaration.


    Code:
    typedef unsigned char  u_char;
    /* define unsigned char as u_char */
    typedef unsigned short u_short;
    /* define unsigned short as u_short */
    typedef unsigned int u_int;
    /* define unsigned int as u_int */
    typedef unsigned long int ul_int;
    /* define unsigned long int as ul_int */
    int main(){
        u_char name = 255;
        u_short Squarfeet = 65535;
    
        
        
    
     }
    is this correct?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Deja Vu!

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does gcc hate Forward declarations?
    By SevenThunders in forum C++ Programming
    Replies: 12
    Last Post: 03-16-2009, 02:03 PM
  2. mixing code with declarations
    By robwhit in forum C Programming
    Replies: 12
    Last Post: 01-31-2008, 12:55 AM
  3. Declarations
    By slippy in forum C++ Programming
    Replies: 13
    Last Post: 12-01-2007, 05:02 PM
  4. portable declarations
    By Marv in forum C Programming
    Replies: 10
    Last Post: 04-12-2007, 04:39 PM
  5. Weird function declarations
    By confuted in forum C Programming
    Replies: 6
    Last Post: 11-09-2005, 03:06 PM