Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-21-2005, 07:39 AM   #1 (permalink)
slashdot
Registered User
 
Join Date: Dec 2004
Posts: 43
slashdot is on a distinguished road
? about initializing var's.

I haven't got to this point in C++ for dummies, or if they have, I left it out. But I have been taking notes for about every paragraph, so thats highly unlikely. Anyways, in the cplusplus.com's c++ tutorial, they get to a part where it talks about initializing variables. CODE: Snipet...

Initialization of variables
When declaring a local variable, its value is undetermined by default. But you may want a variable to store a concrete value the moment that it is declared. In order to do that, you have to append an equal sign followed by the value wanted to the variable declaration:
type identifier = initial_value ;
For example, if we want to declare an int variable called a that contains the value 0 at the moment in which it is declared, we could write:
int a = 0;
Additionally to this way of initializating variables (known as c-like), C++ has added a new way to initialize a variable: by enclosing the initial value between parenthesis ():

type identifier (initial_value) ;
For example:
int a (0);
Both ways are valid and equivalent in C++.

... What exactly are they used for? Could someone give me a more detailed description? Sry if my question wasn't clear enough, but I have to go to class now, so thanx in advance.
slashdot is offline   Reply With Quote
Old 04-21-2005, 08:11 AM   #2 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 735
DJMaze is on a distinguished road
uninitialized variables are NULL (nothing)
Code:
int my_var;
Initialized variables contain a value
Code:
int my_var = 100;
You can create a uninitialized variable and initialize it later
Code:
// create a integer variable
int my_var;
// initialize the variable
my_var = 100;
Then you can use 'my_var' anywhere you want and even change the value.
It's like a piece of paper which is the variable and what you write on the paper is the value.
Now if what you wrote on the paper isn't what you want you clear the paper and write down the new value.

Code:
// create a integer variable
int my_var;
// initialize the variable
my_var = 100;
// change the value
if (my_var == 100) {
  my_var = 25;
}
The 'type' tells what kind of variable it is.
int = integer
char = character
bool = boolean (true/false)
float = floating point
etc.

For example the following code throws a error because "error" is not a integer, it is a string.
Code:
int my_var = "error";
To fix this you could use
Code:
char* my_var = "error";
or use
Code:
// C
string my_var = "error";
// C++
string my_var("error");
The * in "char*" means a pointer and a pointer points to a location in memory which will contain thel data.
Since "char" can only contain 1 character char* can contain many and is like
Code:
char my_var[6] = "error";
Where [6] says how "big" it is.
In this case "error" is 5 characters but you must also have a null terminator ('\0') which actualy tells the system that the string has ended.
Code:
my_var[0] = 'e';
my_var[1] = 'r';
my_var[2] = 'r';
my_var[3] = 'o';
my_var[4] = 'r';
my_var[5] = '\0';
char* doesn't tell how "big" where [x] does tell how big it is.
Since this "char" thing is pretty complicated it is adviced to use "string"
DJMaze is offline   Reply With Quote
Old 04-21-2005, 10:08 AM   #3 (permalink)
slashdot
Registered User
 
Join Date: Dec 2004
Posts: 43
slashdot is on a distinguished road
Nice explanation. No really. I totaly understand it (or I believe I do) now. So let me ask you, if I do something like string my_var[6]: "hello", to about the majority of the program, would I save space in memory? Examples: creating games for low ram systems like game boy advance's? (32k of ram) Or would it not really matter? You know what I mean?
slashdot is offline   Reply With Quote
Old 04-21-2005, 10:31 AM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
uninitialized variables are NULL (nothing)
Code:
int my_var;
No. The behaviour of uninitialized arithmic types is undefined in ISO C++.

Initialized variables contain a value
Code:
int my_var = 100;
Ok.

Quote:
You can create a uninitialized variable and initialize it later
Code:
// create a integer variable
int my_var;
// initialize the variable
my_var = 100;
No. There is a distinct difference between initializing and assignment.This very code snippet is a pure example of assignment.

Quote:
Then you can use 'my_var' anywhere you want and even change the value.
DJMaze, you can do better than that.

Quote:
Code:
// create a integer variable
int my_var;
// initialize the variable
my_var = 100;
// change the value
if (my_var == 100) {
  my_var = 25;
}
Once again, assignment is the deal here. No initializing.

Quote:
Code:
int my_var = "error";
To fix this you could use
Code:
char* my_var = "error";
or use
Code:
// C
string my_var = "error";
// C++
string my_var("error");
Both are valid C++ statements. But observe std::string for ISO C++.

Quote:
The * in "char*" means a pointer and a pointer points to a location in memory which will contain thel data.
No. Assuming *my_var is the subject, then my_var is the pointer. * my_var is the dereferenced pointer. This usually is the object.
A pointer stores an address. That means: it can retreive an address. Nothing more. A const literal like a char* is an array, where the dereferenced objects holds the address of the beginning of the array.

And so forth

For detailed intro on initialization vs. assignment read page three of my tutorial here:
http://cpp.codenewbie.com/articles/c...ee-Page_3.html
__________________
Valmont is offline   Reply With Quote
Old 04-21-2005, 05:04 PM   #5 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 735
DJMaze is on a distinguished road
Quote:
Originally Posted by Valmont
DJMaze, you can do better than that.
I know but my main language isn't english so sometimes i tell it wrong although i do know what each is.
I will keep my mouth shut on explenations to avoid any future misunderstandings in here

Will stick to post code and PHP stuff
DJMaze is offline   Reply With Quote
Old 04-21-2005, 05:20 PM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
C++ has a steep learning curve, and when you end up in a strict ISO/ANSI forum like this one, then things become only harder. Nevertheless, this "strict" forum exists so students learn to think harder about what they are doing and what they are using.

Best thing is to wait for some time and observe. Then try to see if you can be an aid. If you post, and you can't prove things, then at least notify the student so he knows the domain of the answer. Another idea is to try to solve the questions of visitors, but to post your code for evaluation purposes (self test!) only. This way we won't confuse the visitor, and you'll get some extra training.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
java includes and vars sde Java 6 05-03-2004 07:16 PM


All times are GMT -8. The time now is 10:35 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting