LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-10-2009, 08:41 AM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Rep: Reputation: 30
c++ find size of array, through pointer


Hi,
i am trying to find the size of an array, not by using the array as a parameter to "sizeof", but by using a pointer -pointing to the array- as a parameter. How do i do this? I use a 32-bit PC.
Here's:

Code:
#include <string>
#include <iostream>

using namespace std;

struct aStruct {
    int i1;
    string s;
};

aStruct a1={1,"1234"};
aStruct a2={12,"1234f"};
aStruct a3={21312431,"h"};

aStruct ar[]={a1,a2,a3};
aStruct* ap=ar;


int main() {
    cout << sizeof(aStruct) << endl; //prints 8
    cout << sizeof(a1) << endl;  //prints 8
    cout << sizeof(ar) << endl;  //prints 24
    cout << sizeof(ar)/sizeof(aStruct) << endl; //prints 3
    /********Everything OK until here***********/
    cout << sizeof(ap) << endl; //prints 4; the size of a pointer
    cout << sizeof(*ap) << endl; //prints 8; the size of aStruct
}
 
Old 03-10-2009, 08:47 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190
Quote:
Originally Posted by nocturna_gr View Post
i am trying to find the size of an array, not by using the array as a parameter to "sizeof", but by using a pointer -pointing to the array- as a parameter. How do i do this?
You can't.

That is one of several reasons one might choose to use a std::vector instead of an array.
 
Old 03-10-2009, 12:59 PM   #3
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by nocturna_gr View Post
Hi,
i am trying to find the size of an array, not by using the array as a parameter to "sizeof", but by using a pointer -pointing to the array- as a parameter. How do i do this? I use a 32-bit PC.
YOu can't do that. Either store array size in constant, or mark end of array somehow.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
about array and pointer in c bulkathos Programming 19 05-18-2009 09:06 PM
how to find out an array size on c++ poeta_boy Programming 9 06-22-2004 02:28 PM
about array and pointer seeLnd Programming 5 05-30-2004 08:56 AM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 03:24 PM
pointer to a 2D array dhanakom Programming 7 09-07-2003 05:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration