-
January 6th, 2014, 01:56 PM
#1
Dynamic Array Length ?
Hello,
How can i change or add a cell to an array without knowing its length ? (I mean that I shouldn't know the length I need to make the length dynamic so i can add a cell)we just learned Dynamic assignment(I hope this is the correct name) and Pointers.
Thank you
-
January 6th, 2014, 02:11 PM
#2
Re: Dynamic Array Length ?
You would need to know the length or you could use the built in dynamic array, vector.
-
January 6th, 2014, 02:54 PM
#3
Re: Dynamic Array Length ?
Originally Posted by ron_a
How can i change or add a cell to an array without knowing its length ?
Have a look at CArray class and similar classes here
Victor Nijegorodov
-
January 6th, 2014, 02:57 PM
#4
Re: Dynamic Array Length ?
You can change an element of an array by knowing its position in the array. As you are just changing the contents of the element, it doesn't change the size of the array. So providing you access a valid element (and c++ doesn't provide a check for this) you need to know its length to make sure the access is valid.
If you want to add a cell to an array, it depends upon how the array is implemented. To provide further help and guidance could you explain further what you mean by 'an array'. As you mentioned Dynamic Assigment, this seems like you are using dynamically allocated memory for the array? The STL vector is an array internally implemented via dynamically allocated memory. So using vector you can add elements to the end of the vector without knowing its size as the vector implementation deals with the dynamic memory requirements.
Can you clarify a bit more as what to are trying to do or want to know?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 7th, 2014, 11:19 AM
#5
Re: Dynamic Array Length ?
Specifically, I think you are looking for CArray::SetAtGrow, which will dynamically enlarge the array.
http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx
Nobody cares how it works as long as it works
-
January 13th, 2014, 05:40 AM
#6
Re: Dynamic Array Length ?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|