473,385 Members | 1,736 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.

Easy question!

52
Hey guys,

This should be easy but I need a little help.

This is in C... I have an array or ints I need to put something at the end that I can find to stop a loop... I tried using '\0' or setting it to NULL, but those seem to translate to 0... which doesn't work for me.

any help is appreciated!
Sep 18 '08 #1
6 1475
Ganon11
3,652 Expert 2GB
An easy solution is to use a dummy value like -9999, but that may not be a very satisfactory solution for you.

Why do you have an integer-filled array with a size larger than the actual number of elements in it?
Sep 19 '08 #2
Laharl
849 Expert 512MB
I tried using '\0' or setting it to NULL
NULL is 0 and '\0' is the null character, with ASCII code 0. Anything you choose as a sentinel had better not appear in the array or you'll get premature termination. Perhaps INT_MIN, defined in <limits.h>, if you're working with signed ints, or INT_MAX if you aren't? (If you don't know, you're using signed).
Sep 19 '08 #3
Shisou
52
hmm... i think this is going to be too complicated to find an easy solution to... and i have one that isn't perfect but it's close enough...

Thanks anyways guys... unfortunately it seems a work around is the best i can do
Sep 19 '08 #4
JosAH
11,448 Expert 8TB
Alternatively you can store the index position of the element in the array that
represents the logical end of the array. Of course you can't store that index
position in the array itself.

kind regards,

Jos
Sep 19 '08 #5
Banfa
9,065 Expert Mod 8TB
You should not really need to use a sentinel value to stop a loop iterating through an array of ints.

Either it is a true array in which case you can get the endpoint using the sizeof operator.

Or it is a pointer, however at some point you knew the size of the array, either it was a true array and you have passed a pointer to it to another function or it was malloced some time in which case at the time you malloced it you new its size.

It you are passing a pointer to an array around functions then pass its size around too.
Sep 19 '08 #6
weaknessforcats
9,208 Expert Mod 8TB
Read this article: http://bytes.com/forum/thread772412.html

Arrays as a rule do not have sentinal values. You have to separately know the number of elements in the array.

All that \0 does is make a char array useable in a special context. I suppose you could do the same with your int array if you chose a value that is outside the range of your data.
Sep 19 '08 #7

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

Similar topics

1
by: rdsteph | last post by:
I am having a lot of fun using the pyGoogle module ( http://pygoogle.sourceforge.net/ ) that uses the Google API. It is about as easy to use as I can imagine, and it is a lot nicer than using my...
0
by: | last post by:
I don't know SQL at all, but I have a problem now because I must use SQL in my PHP scripts. So please help me! I have 2 tables: id | name ----------- 1 | thing1 2 | thing2 3 | thing3 4 ...
5
by: LedZep | last post by:
What up, All I need to do is enter a last name in a text box, query a MSAccess database and display the name with the corresponding columns. This is no problem, but when there are more than one...
1
by: Mad Scientist Jr | last post by:
can someone explain how to simply populate a grid in .net ? the way i understand it, there is no more msflexgrid, and instead is this new control that has to be tied to a dataset, and it is a real...
13
by: Ghislain Tanguay | last post by:
I have a compiled vb.net app and I want to give the user a choice to launch it from the start line command and pass it a parameter or not. How can I do that in my code? Is it possible? Ex. :...
1
by: melanieab | last post by:
Hi, If there's a textbox and the text entered is longer than what's visible (the textbox length), how do you make it so that the beginning chunk of text is visible (instead of the last part of...
23
by: **Developer** | last post by:
Is there an easy way to copies all files in a directory into another directory? What about coping subdirectories too? Thanks in advance for any info
6
by: Martin Bootsma | last post by:
I have a C question, which looks very easy, but no one here seems to know an easy answer. I have a function "powell" (from Numerical Recipes) which takes an argument of the type "double...
27
by: smnoff | last post by:
How does( or should user use) strncpy() to allocate enough memory space for the destination string, s1? I guess I am having trouble using strncpy as it seems to be giving me errors. And just...
9
by: rynTAU | last post by:
This is an easy question, I'm sure but I can't seem to figure it out. I'm trying to use the number_format() function to remove all decimal places from a number. $num = number_format($num,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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.