473,382 Members | 1,373 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,382 software developers and data experts.

Pointer Artithmetic Puzzle

Hi,
I just came across this issue in my code and, while it isn't an issue
any more because the whole algorithm had to be changed, it is still
bothering me because I don't understand why it should be so.

I am using a struct type that has some data as well as prev and next
pointers to implement linked list functionality.
I pre-allocate a block of structs (called fieldIndex)
malloc(n * sizeof(fieldIndex));
and then arrange the pointers internally to be consistent.
Then later on I want to find out if the current prev pointer is in the
same block. To start with I used:
current->prev == current - 1
thinking that pointer arithmetic would be invoked and all would be
well. It wasn't so I tried this:
current->prev == current - sizeof(fieldIndex)
and it worked just fine.
I use pointer arithmetic quite a bit and I cannot explain why I have to
invoke sizeof in this case. I looked at K&R and I couldn't see anything
that would suggest a difference between assignment and comparison
operations or any other rule for this case but I couldn't see anything.
I also tested under gcc and MSVC it is consistent, fyi.
Can anyone shed any light on this?

Cheers,
Charlie.

Mar 28 '06 #1
1 1718
Ok so I'm an idiot and there is another bug that's causing this not to
work but making it appear that pointer arithmetic is the problem argh!
At some point I will learn to wait an hour or so to post to usenet then
I won't have to publiclly call myself an idiot so much!

Cheers,
Charlie

Mar 28 '06 #2

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

Similar topics

1
by: Antonio | last post by:
Hope that some perl guru will help me: I've implemented an inverted index using PERL + DB_File (using hash, and b-tree). Therefore, i have my indexer and my searcher (a little search engine,...
0
by: Anupam | last post by:
In article <D8sq9J.HKx@alamo.net>, Briggs Reschke <briggs@alamo.net> wrote: >The following code fragment is a little puzzle a friend passed on to me ( >The origionators name is Christopher...
4
by: Joe H | last post by:
Hi i converted a program to a lib. The parameters of main are maincommand(int argc, char **argv) I want to use main a s function that takes a string how can i convert a char * to a char ** or even...
1
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
0
by: xavier vazquez | last post by:
have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the...
5
by: ashish0799 | last post by:
HI I M ASHISH I WANT ALGORYTHMUS OF THIS PROBLEM Jigsaw puzzles. You would have solved many in your childhood and many people still like it in their old ages also. Now what you have got to do...
3
by: oncue01 | last post by:
Word Puzzle Task You are going to search M words in an N × N puzzle. The words may have been placed in one of the four directions as from (i) left to right (E), (ii) right to left (W), (iii) up...
13
by: Prisoner at War | last post by:
I know about the * * style="cursor: pointer; cursor: hand;" * * attribute of the <imgtag, but is there a way JavaScript can "load in" one's own graphic for such events?? How? TIA!
4
by: honey777 | last post by:
Problem: 15 Puzzle This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle: The goal is to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
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...

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.