Thread: Assignment vs. Initialization

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    31

    Assignment vs. Initialization

    I'm studying for an exam and just came across a sentence: "Initialization isn't the same as assignment." What? I thought when you assigned a variable a value, you're initializing the variable. If not, could someone clarify assignment vs initialization?

    Thanks!

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    134
    There is defference between defination and declaration its same as initaililization and assignment, when u initialize a variable it means u r making identify the variable to your compiler, but when you assign you get the adress assigned for that variable for your compiler.
    e.g. Intialization
    Code:
    int i;
    assignment
    Code:
    int i=0;
    or in OOPS

    Initialization
    Code:
    Object obj;
    Assignment:

    Code:
    Object obj = new Object( arg list );

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is not true.
    This is also the C board, no such things as classes which is a C++ term.

    Code:
    int i;
    No initialization.

    Code:
    int i = 0;
    Initialization to 0.

    Code:
    int i; */ Initialization */
    i = 0; /* Assignment */
    I don't think initialization vs assignment has such a big impact in C. More so in C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, and there are several cases where the difference is noticeable, particularly when it comes to structures and arrays.

    You can initialize an array or struct with constant values, e.g. :
    Code:
    struct blah
    {
       int x;
       char *s;
    };
    
    struct blah array[3] = { { 1, "One"}, {2, "Two"}, { 3, "Three" } };
    
    // However, this doesn't work:
    struct blah array[3];
    
    array = { { 1, "One"}, {2, "Two"}, { 3, "Three" } };
    
    // Also won't work:
    struct blah array[3];
    struct blah arr2[3];
    ...
    array = arr2;
    
    // This works, however
    array[1] = arr2[1];
    That covers both structure and array initialization. Essentially, the compiler is able to give a variable a value when it's defined, but not later - you can only assign one element at a time from an array for example, and you can not assign constant values directly to a struct as a list of element values, each element on it's own will have to be set.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    The problem with using syntax alone to define the term is that:
    Code:
    int i;
    would not be initialized and thus have an indeterminate value if i has automatic storage duration, but would be initialized to 0 if i has static storage duration.

    Perhaps we should just state:
    Initialization means providing an initial value to an object (where object means "a contiguous region of memory holding a value of some type"). Assignment means providing a new value to an object that has been initialized or that has an indeterminate value.
    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

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    Oooh. So initialization is basically creating a variable and assigning it a value all in go, and assignment is changing the value of an already existing variable. It makes more sense when you say it like that.
    Last edited by arrgh; 05-06-2008 at 03:12 AM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes. Or creating a variable with a value (initialization).
    I think laserlight's definition of initialization is a good one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Initialization and assignment
    By rajesh1978 in forum C++ Programming
    Replies: 7
    Last Post: 03-18-2009, 08:13 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM