problems with []
hi all. i''ve already seen lots of sourcecodes with something like
int units[12];
this is not supposed to be "int units = 12", isn''t it? if i demand units to be added to another int, it returns an error message saying units and i would be of an incompatible type. and if i write out units with printf i get a "segmentation fault" during the program runs. so what is this "[12]" supposed to mean?
or what is "" ???
some codes also do have some fuzzy things like "int *i" or "&i". i looked in the internet for explanation and fount out that & is meant as an binary AND. nut what has a binary AND have to search in "return &i" ? i suppose that for a binary AND there are two values needed to opperate. so what is the meaning of "return &i"? and what the one of "int *i"?
this C++ stuff is really disappointing me
February 07, 2003 12:33 PM
Well the [] is commonly refered to as the array operator. This creates a number of the same objects that are accessable as if it were a stack of alphabet blocks. int array[12]; creates an array of 12 integer variables numbered 0 through 11. You can access each individual variable like array[0] = 5; array[1] = 2; and so on.
The & operator has 2 purposes in C++. It''s used as the binary AND operator for operations like int val = 0x80; int val2 = 0x10;
int result = val & val2;
But & also has another purpose it''s the reference operator. This operator is used to pass parameters into functions. Commonly it''s main use is when you want to pass in a value and modify it''s result with out having to return the value. So instead of int increment(int value) { return value +=1; } int myValue = 10; myValue = increment(myValue); you can do void increment(int &value) { value += 1; } increment(Myvalue); and get 11 as the result.
the * operator does multiple things as well, it''s the multiply operator for operatons like value * value; but when you see it like int *i; then it''s considered a pointer.
A pointer is the address of the variable your trying to access. This pointer can be passed to and refrenced anywhere in your program and can point to multiple variables if you ever needed to change it. It''s similar to the address on your house, you use it to locate a specific object, and if you want to access another object you just change the address the pointer is pointing towards.
Pointers and refrences will take a while to understand, just follow some pointer and refrence tutorials and you''ll eventually pick it up.
The & operator has 2 purposes in C++. It''s used as the binary AND operator for operations like int val = 0x80; int val2 = 0x10;
int result = val & val2;
But & also has another purpose it''s the reference operator. This operator is used to pass parameters into functions. Commonly it''s main use is when you want to pass in a value and modify it''s result with out having to return the value. So instead of int increment(int value) { return value +=1; } int myValue = 10; myValue = increment(myValue); you can do void increment(int &value) { value += 1; } increment(Myvalue); and get 11 as the result.
the * operator does multiple things as well, it''s the multiply operator for operatons like value * value; but when you see it like int *i; then it''s considered a pointer.
A pointer is the address of the variable your trying to access. This pointer can be passed to and refrenced anywhere in your program and can point to multiple variables if you ever needed to change it. It''s similar to the address on your house, you use it to locate a specific object, and if you want to access another object you just change the address the pointer is pointing towards.
Pointers and refrences will take a while to understand, just follow some pointer and refrence tutorials and you''ll eventually pick it up.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement