473,385 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help needed in solving this problem

#include<stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};

int main()
{
int d;

for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);

return 0;
}

The above snippet doesnt print the array as expected.
I think the error is in the Test-condition in for loop.
If i cast it to INT [(int)TOTAL_ELEMENTS-2)] than its working fine.
y is it so..can sbd tell me what the return type of sizeof
is??

Nov 15 '05 #1
5 1123
In article <11**********************@g44g2000cwa.googlegroups .com>,
sasiraj <va*********@gmail.com> wrote:
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0])) int d; for(d=-1;d <= (TOTAL_ELEMENTS-2);d++) I think the error is in the Test-condition in for loop.
If i cast it to INT [(int)TOTAL_ELEMENTS-2)] than its working fine.
y is it so..can sbd tell me what the return type of sizeof
is??


sizeof has type size_t . The exact width of size_t is unspecified,
but one thing that -is- specified about it is that it is unsigned.
You might be having difficulty with comparing a negative int
to an unsigned integral type.
--
"This was a Golden Age, a time of high adventure, rich living and
hard dying... but nobody thought so." -- Alfred Bester, TSMD
Nov 15 '05 #2
In article <11**********************@g44g2000cwa.googlegroups .com>,
sasiraj <va*********@gmail.com> wrote:
#include<stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};

int main()
{
int d;

for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);

return 0;
}

The above snippet doesnt print the array as expected.
I think the error is in the Test-condition in for loop.
If i cast it to INT [(int)TOTAL_ELEMENTS-2)] than its working fine.
y is it so..can sbd tell me what the return type of sizeof
is??


From K&R2, Section A6.5:

[automatic conversion of operands]
If either operand is unsigned int, the other is
converted to unsigned int.

Thus the -1 in d=-1 is converted (unsigned int)(-1) which
generally is a pretty large number.

--
Rouben Rostamian
Nov 15 '05 #3
ice


sasiraj wrote:
#include<stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};

int main()
{
int d;

for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);

return 0;
}

The above snippet doesnt print the array as expected. hello sasi
Do you want to print the array in order as it is declared?If yes they
are printing in order .
and if no then clarify in which order? I think the error is in the Test-condition in for loop.
If i cast it to INT [(int)TOTAL_ELEMENTS-2)] than its working fine.
y is it so..can sbd tell me what the return type of sizeof
is??


Nov 15 '05 #4
sasiraj wrote:
#include<stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0])) Just make it:
#define TOTAL_ELEMENTS (signed)(sizeof(array) / sizeof(array[0]))

Try going through sizeof help and you would have the answer yourself.
int array[] = {23,34,12,17,204,99,16};

int main()
{
int d;

for(d=-1;d <= (TOTAL_ELEMENTS-2);d++) OR another choice could be to initialise d to 0. But I guess you did
not want this. Soo, go through sizeof help.
printf("%d\n",array[d+1]);

return 0;
}

The above snippet doesnt print the array as expected.
I think the error is in the Test-condition in for loop.
If i cast it to INT [(int)TOTAL_ELEMENTS-2)] than its working fine.
y is it so..can sbd tell me what the return type of sizeof
is??


Nov 15 '05 #5
ice wrote:
sasiraj wrote:

[snip program]
The above snippet doesnt print the array as expected.

hello sasi
Do you want to print the array in order as it is declared?If yes they
are printing in order .
and if no then clarify in which order?
I think the error is in the Test-condition in for loop.
If i cast it to INT [(int)TOTAL_ELEMENTS-2)] than its working fine.
y is it so..can sbd tell me what the return type of sizeof
is??


I dont think its should print the contents without you modifying the
#define preprocessor. Please check once again.

OR probably I need to check my compiler. ;)

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Sims | last post by:
Hi I have a table with something like ID PARENT 0 | -1 1 | -1 2 | 1 3 | 1
13
by: XXXXXX.working.in.my.blood | last post by:
hi all, i need help with linked lists... the problem is this, "reverse the contents of a singly linked list without using a temporary node"... solution with code will be appreciated...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
4
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
6
by: naknak | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
10
by: kickioly | last post by:
Hi everyone, this is my first time here and i hope it will not be the last, i have a problem here solving this C++ problem,and i hope that someone here to solve it and i'll be happy if you do it...
8
by: Mr. SweatyFinger | last post by:
where have they all gone. alt.suicide? alt.findContol.then.slit.your.own.throat?
5
by: jvdb | last post by:
Hi there, I am quite new on python programming and need some help on solving my problem.. I have to make a (python) program which deletes files from directories. I don't think deleting, etc....
3
by: NatRoger | last post by:
Hi, We are using DB2 V7 (MVS) in our shop, which dont have the luxury of the 'WITH clause' - CTE support to accomplish recursive queries. Here is my problem - Table1 From-ID To-ID A1 ...
118
by: Chuck Cheeze | last post by:
This might be in the wrong group, but... Here is an example of my data: entry_id cat_id 1 20 2 25 3 30 4 25 5 35
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.