C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 14th, 2003, 11:58 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
Send a message via AIM to andy3109
finding size of integer array?

strlen doesn't work for finding the size of a integer array does it? I have something like this:

int x[] = {1,4,33,4};
int y = strlen(x);

cout << y;

I am expecting Y to say "4" but my compiler just gives me a weird error. Ideas? Im on windows2000, MSVC..Thanks.

-andy
__________________
hmmm...

Reply With Quote
  #2  
Old June 15th, 2003, 12:49 AM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 94
if an integer is 4 bytes, then an array of 4 integers would be 16 bytes long. here from this example we can see:
Code:
#include<stdio.h>

int main(int argc, char **argv)
{
        int array[4] = {1,2,3,4};

        printf("%p %p %p %p\n", &array[0], &array[1], &array[2], &array[3]);

        unsigned int diff = (unsigned int)&array[3] - (unsigned int)&array[0];

        printf("diff is %x\n", diff);

        return 1;
}

it's output reads:
0xbffffab0 0xbffffab4 0xbffffab8 0xbffffabc
diff is c

-so between the first and last element there is 12 bytes. + an extra 4 for teh last element and we have 16

Last edited by infamous41md : June 15th, 2003 at 12:56 AM.

Reply With Quote
  #3  
Old June 15th, 2003, 01:32 AM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
Send a message via AIM to andy3109
hmm..maybe i didn't explain myself right. I don't care how many bytes are in the array..i just wanna know if there is a way I can find out how many elements an integer array has in it. My first example explains that pretty well. Thanks for your input though i actually learned something new through it.

-andy

Reply With Quote
  #4  
Old June 15th, 2003, 02:26 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,390 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 36 m 15 sec
Reputation Power: 4080
Use the sizeof operator instead:
int x[] = {1,4,33,4};
int y = sizeof (x) / sizeof (x[0]);

Note that this will not work if your arrays is allocated with malloc, calloc, new, strdup or anything like that.

Reply With Quote
  #5  
Old June 15th, 2003, 06:37 AM
BigBadBob BigBadBob is offline
status unknown
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 262 BigBadBob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
Of course if you're using C++, as the OP was, then you can use a vector instead of an array, Then vector.size() gives you the number of elements. It's worth considering using vector instead of array as a general case.

Reply With Quote
  #6  
Old June 15th, 2003, 09:47 AM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
Send a message via AIM to andy3109
correct, I can also use a matrix. But for some reason it seems that vector's and matrix's arent used often. Why is this?

Reply With Quote
  #7  
Old June 15th, 2003, 10:12 AM
BigBadBob BigBadBob is offline
status unknown
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 262 BigBadBob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
I'm not sure why you think vectors aren't used often, but in my experience they are. I think for historical reasons many courses teach arrays before vectors, or teach arrays only, but for general use I would suggest using vectors (or generalising, you should prefer to use the containers in the C++ standard library). You should still know how to use both, of course.

Reply With Quote
  #8  
Old June 15th, 2003, 12:03 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,142 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 34 m 16 sec
Reputation Power: 1974
Re: finding size of integer array?

Quote:
Originally posted by andy3109
strlen doesn't work for finding the size of a integer array does it? I have something like this:

int x[] = {1,4,33,4};
int y = strlen(x);

cout << y;

I am expecting Y to say "4" but my compiler just gives me a weird error. Ideas? Im on windows2000, MSVC..Thanks.

-andy

No, strlen does not work. While everybody else have given you good info on solving your problem, they did not address your basic mistake.

The thing is that strlen works on character arrays and depends on basic assumptions about C-style strings (AKA "char arrays"), especially that they be null-terminated. Because of those basic assumptions, trying to use strlen on non-char arrays will yield unexpected results, which is what you got.

Bits are bits and bytes are bytes. There is no intrinsic difference between characters, integers, floating-point numbers, pointers, or machine instruction op codes; if you see a byte taken out of context, you will have no idea what data type it is a part of. We tell the computer through our program how it will interpret those bytes.

So if you tell the computer that that integer array is an array of characters, then it will interpret each byte on each integer as a character (in reverse byte order on an Intel). strlen will count each "character" until it finds the null-terminator, a byte containing the value 0x00.

But that would only have been if you could have gotten your program to run. You say you got a weird error. Did it have anything to do with trying to feed strlen an int pointer when it expects a char pointer?

Reply With Quote
  #9  
Old June 15th, 2003, 06:01 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
Send a message via AIM to andy3109
that actually was the error. But it was related to using strlen with an int pointer.

Reply With Quote
  #10  
Old June 15th, 2003, 06:06 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
Send a message via AIM to andy3109
ok, im running into problems. I designing a sort of a sort algorithm that is very similar to bubble sort...The output, nothing just a blank dos screen that has a blinker on it. It is related to the "sizeof" line im possitive.. Here is the code:

int i=0,x,temp;
float thearray[] = {90,54,90,33,44,56, '\n'};
float amt;
amt = sizeof(thearray);
for (x = 0; x<amt; x++)
{
for (i=0; i<amt; i++)
{
if (thearray[i] > thearray[i+1])
{

temp = thearray[i+1];
thearray[i+1] = thearray[i];
thearray[i] = temp;
}
}
}
for (i=0; i<amt; i++)
{
cout << thearray[i] << "\n";
}
system("pause");

}

Reply With Quote
  #11  
Old June 15th, 2003, 09:10 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,390 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 36 m 15 sec
Reputation Power: 4080
You're computing amt incorrectly.
amt = sizeof (thearray) / sizeof(thearray[0]);

BTW, shouldn't amt be of type int too. Also how did you manage to get the compiler to not complain about '\n' in the declaration of thearray

Reply With Quote
  #12  
Old June 15th, 2003, 10:19 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
Send a message via AIM to andy3109
idk..it doesn't complain though

Why "/ sizeof(thearray[0]);"? What does that do?

Last edited by andy3109 : June 15th, 2003 at 10:21 PM.

Reply With Quote
  #13  
Old June 15th, 2003, 11:56 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 94
Code:
float array[5] = {1.0,2.1,3.3,4.4,5.5};
int size = sizeof(array)/sizeof(float);

printf("size is %d\n", size);

-when you do sizeof(array) it returns to you the total size in bytes of your array. what you said you wanted is the number of elements in the array. so you must divide the total size by the size of each element. this is what scorpions was showing you. you could also do it the way i put above.

Reply With Quote
  #14  
Old June 16th, 2003, 12:13 AM
3dfxMM 3dfxMM is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 272 3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 6 Days 17 h 57 m 24 sec
Reputation Power: 17
Quote:
Also how did you manage to get the compiler to not complain about '\n' in the declaration of thearray

'\n' is just another byte value. The compiler shouldn't complain about it. What I am curious about is what purpose does it serve in an array of floats?

Reply With Quote
  #15  
Old June 16th, 2003, 08:34 AM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
Send a message via AIM to andy3109
well originaly I used i+1 for my algorithm which was flawed because on the last test to organize the #ers within the array, there was nothing to compare. So I compared it with a null character (which didn't effect order). But I ended up just switching to i-1 and got rid of the ending '\n'.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > finding size of integer array?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap