| | |
Finding the length of an int pointer array
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 36
Reputation:
Solved Threads: 4
I'm working on a little code snippet and my c++ seems a bit rusty. How do i go about getting the length of an array? My eclipse is giving me weird values when it should be segfaulting, so I'm trying to figure out whats wrong. Thanks in advance.
edit: forgot to say what i meant by an int array.
int *x = new int[8];
edit: forgot to say what i meant by an int array.
int *x = new int[8];
Last edited by DaBunBun; Jul 28th, 2009 at 2:17 pm.
•
•
Join Date: Feb 2008
Posts: 638
Reputation:
Solved Threads: 46
I always advocate using an std::vector when possible.. can you do this:
C++ Syntax (Toggle Plain Text)
std::vector<int*> yourVar(8);
•
•
Join Date: Jul 2009
Posts: 36
Reputation:
Solved Threads: 4
Are you saying to use a different variable type?
Or are you saying to type cast it?
If its the first, I want to get the length of a pointer thats being passed.
Edit: Forgot to say, thanks for the help. Normally I'd use a vector like that, except in this case I'm being passed an in pointer.
Or are you saying to type cast it?
If its the first, I want to get the length of a pointer thats being passed.
Edit: Forgot to say, thanks for the help. Normally I'd use a vector like that, except in this case I'm being passed an in pointer.
Last edited by DaBunBun; Jul 28th, 2009 at 2:30 pm.
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
Are you saying to use a different variable type?
Or are you saying to type cast it?
If its the first, I want to get the length of a pointer thats being passed.
Edit: Forgot to say, thanks for the help. Normally I'd use a vector like that, except in this case I'm being passed an in pointer.
You always have to pay attention when you create a vector of pointers, consider this:
When variable a goes out-of-scope, then the first pointer in vector myvec (myvec[0]) will point to some memory location which doesn't belong to your program anymore.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <vector> using namespace std; int main() { vector<int*> myvec(5); if( 5 > 3 ) { int a = 35; myvec[0] = &a; cout << *myvec[0] << endl; } cout << *myvec[0] << endl; // not guaranteed to print 35 on the screen return 0; }
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
![]() |
Similar Threads
- Pointer to array problem (C++)
- Length of an int array (C++)
- Pointer Array to templates (C++)
- Return pointer to array of a struct (C++)
- Passing a pointer to an array. (C)
- Pointer to array (C++)
- Finding length (Java)
- Declaration of dynamic pointer array puzzle. (C)
- structures containing a pointer to an array (C++)
Other Threads in the C++ Forum
- Previous Thread: Most popular graphics library
- Next Thread: what is produced in an inflie function?
Views: 548 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






