Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 4th, 2006, 6:23 PM   #1
b1g4L
Programmer
 
Join Date: Dec 2005
Location: SC
Posts: 38
Rep Power: 0 b1g4L is on a distinguished road
Send a message via AIM to b1g4L
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
b1g4L is offline   Reply With Quote
Old Feb 4th, 2006, 6:34 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Feb 4th, 2006, 7:30 PM   #3
b1g4L
Programmer
 
Join Date: Dec 2005
Location: SC
Posts: 38
Rep Power: 0 b1g4L is on a distinguished road
Send a message via AIM to b1g4L
Yea, 1 byte (sorry). Does the +1 move to the next spot in memory (in the array) then?
b1g4L is offline   Reply With Quote
Old Feb 4th, 2006, 7:33 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Feb 4th, 2006, 10:48 PM   #5
b1g4L
Programmer
 
Join Date: Dec 2005
Location: SC
Posts: 38
Rep Power: 0 b1g4L is on a distinguished road
Send a message via AIM to b1g4L
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?
b1g4L is offline   Reply With Quote
Old Feb 4th, 2006, 10:57 PM   #6
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
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!
rsnd is offline   Reply With Quote
Old Feb 4th, 2006, 11:03 PM   #7
b1g4L
Programmer
 
Join Date: Dec 2005
Location: SC
Posts: 38
Rep Power: 0 b1g4L is on a distinguished road
Send a message via AIM to b1g4L
I tried that and I dont get a hex address value but instead funny symbols.
b1g4L is offline   Reply With Quote
Old Feb 4th, 2006, 11:10 PM   #8
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
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!
rsnd is offline   Reply With Quote
Old Feb 4th, 2006, 11:25 PM   #9
b1g4L
Programmer
 
Join Date: Dec 2005
Location: SC
Posts: 38
Rep Power: 0 b1g4L is on a distinguished road
Send a message via AIM to b1g4L
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.
b1g4L is offline   Reply With Quote
Old Feb 5th, 2006, 2:33 AM   #10
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5 grumpy is on a distinguished road
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;
}
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:00 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC