473,465 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

regarding array

7 New Member
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int arr[] = {1, 2, 3, 4, 5};
  3. #define NUMBER_OF_ELEMENTS (sizeof(arr) / sizeof(arr[0]))
  4. int main()
  5. {
  6.         int index = -1;
  7.         if (index <= NUMBER_OF_ELEMENTS - 3)
  8.                 printf( "A problem is not a problem if it CAN be solved\n"
  9.                         "A problem is not a problem if it CANNOT be solved\n");
  10.         else
  11.                 printf("Time stays long enough for anyone who will use it\n");
  12.         return 0;
  13. }

can anyone give the explanation to why is the output "Time stays long enough for anyone who will use it."
May 17 '07 #1
3 1026
Ganon11
3,652 Recognized Expert Specialist
Moving this to the C++/C forum.
May 17 '07 #2
Ganon11
3,652 Recognized Expert Specialist
My guess is that, in your define statement, sizeof(arr) is not giving you the results you are expecting. It is probably calculating the size of the pointer, not the array. This means you are dividing a small value by a larger value (since an integer takes up more memory than a pointer) and getting 0.
May 17 '07 #3
AdrianH
1,251 Recognized Expert Top Contributor
My guess is that, in your define statement, sizeof(arr) is not giving you the results you are expecting. It is probably calculating the size of the pointer, not the array. This means you are dividing a small value by a larger value (since an integer takes up more memory than a pointer) and getting 0.
No, it is doing it correctly. Turn on the warnings of your compiler. You will see a comparison warning.


Adrian
May 17 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: chris | last post by:
What is a class ?? is it like a function ?? this has allways confused me as i am a newby to programming (since Basic in the 80's) thanks for any insight you can give
7
by: Squignibbler | last post by:
Hi all, I have a question regarding the C++ programming language regarding the nature of the relationship between pointers and arrays. If the statement MyArray is functionally identical to...
9
by: niclane | last post by:
Hi, I was reading section 5.5 of Ritchie and Kernighan and saw the following: " ..... char amessage = "now is the time"; char *pmessage = "now is the time";
5
by: Wajih-ur-Rehman | last post by:
The question is about C++ (since its the C family, i posted it on this newsgroup) Lets say i declare an array int a = {1,2,3,4}; int *p = a; //This is allowed because "a" returns the address of...
3
by: trialproduct2004 | last post by:
Hi all i am having application in c#. I want to create array of class object at runtime. I don't want to set array bound at design time. How to set array bound at runtime and how to access array...
12
by: ravinderthakur | last post by:
hi experts, i have few questions regarding the delete operator in c++. why does c++ have to operators for deleting memeory viz delete and delete. why cannnot delete be used insted of...
2
by: John Smith | last post by:
Hi, I have a question regarding the initialisation of aggregates: The C (99) standard states: section 6.7.8, paragraph 21 states: If there are fewer initializers in a brace-enclosed list than...
4
by: Michel Esber | last post by:
Hello, Environment: db2 V8 FP 13 LUW Our application currently uses: insert into table values ('A'),('B'),...('Z') We have used CLI arrays inserts (1000 array and commit size) and...
1
by: vinay3744 | last post by:
Hello I have this code string **a; //pointer to pointer to a string int x=300; int y=500;
0
by: yonil | last post by:
I saw a question about how to make an array of vectors in C++. The answer was that it's impossible, because C arrays are non-copyable and non-assignable, and vector< vector<T> > or vector< pair<T, T>...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.