![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
address memory in an array
I have an array of chars (1 bit).
char *array = new char [15]; If I want to set a pointer to the 6th index in that array, does this code do that? char *address = array + 5 |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Yes, but a char is not one bit, it's one byte (8 bits). While pointers to arrays and arrays appear to be the same thing in some contexts, it isn't strictly true. If this seems confusing to you, you might want to refer to the "Pointer Basics" link in my signature, in particular the section "What is not a pointer."
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Programmer
|
Yea, 1 byte (sorry). Does the +1 move to the next spot in memory (in the array) then?
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
It's called 'array arithmetic.' Again, see my tutorial. For a char array it moves one byte. It moves one element. If the elements are integers, it moves the size of one integer, and so forth. That's one reason the pointer has a type.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Programmer
|
How can I get the memory address of a specific index in an array? Say I wanted to know the memory address of array [5]? Does array [5] need to contain data before you can find out its memory location?
|
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4
![]() |
it doesnot need to contain entered data or anything...but it needs to be existant. for the address of array[5] i think you can go &array[5] or array+5 (as array itself is pointer to the addr.)
__________________
Spread your wings and fly! Chicken! |
|
|
|
|
|
#7 |
|
Programmer
|
I tried that and I dont get a hex address value but instead funny symbols.
|
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4
![]() |
I think its because you are outputting it wrong...you are expected to get funny symbols if the adress bytes gets converted into chars.
__________________
Spread your wings and fly! Chicken! |
|
|
|
|
|
#9 |
|
Programmer
|
This worked. Thanks.
cout << &array + 5; Another question....Can a char pointer be set to point to a char array? For instance...... This would be in the main: char *array = new char [10]; Class.add(array); This would be the function receiving 'array' add (char *startOfArray); I want 'array' and 'startOfArray' to have the same memory address (point to the same object). Last edited by b1g4L; Feb 4th, 2006 at 11:49 PM. |
|
|
|
|
|
#10 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5
![]() |
yes, it is possible. However, you need to ensure that the "array" in main() continues to exist, otherwise almost anything that Class does with pointer it receives will cause undefined behaviour.
For example; class MyClass
{
public:
void add(char *startOfArray) {start = startOfArray;};
void manipulate() {start[5] = 42;};
private:
char *start;
};
int main()
{
MyClass Class;
char *array = new char [10];
Class.add(array);
Class.manipulate();
delete [] array; // hence forth, array no longer exists
Class.manipulate(); // undefined behaviour as this modifies array[5]
return 0;
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|