473,503 Members | 11,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem updating a ptr in a switch statement

I try to update the buffer pointer in a switch statement like this:

buf[ptr]=(serialinput);
switch (buf[ptr++])

that doesn't work correctly -- doesn't update the ptr

I changed it to:
buf[ptr++]=(serialinput);
switch (buf[ptr-1])

And now it works, but I don't understand why/why the first doesn't.

Thanks, Tom

Jul 27 '08 #1
2 1541
Blip wrote:
I try to update the buffer pointer in a switch statement like this:

buf[ptr]=(serialinput);
switch (buf[ptr++])

that doesn't work correctly -- doesn't update the ptr

I changed it to:
buf[ptr++]=(serialinput);
switch (buf[ptr-1])

And now it works, but I don't understand why/why the first doesn't.
Both should give the same outcome. Can you (1) explain
why you think the first form does not update ptr, and (2) post
a short, complete, compilable program that demonstrates the
unexpected behavior?

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jul 27 '08 #2
On Jul 27, 10:20 am, Blip <b...@krumpli.comwrote:
I try to update the buffer pointer in a switch statement like this:

buf[ptr]=(serialinput);
switch (buf[ptr++])

that doesn't work correctly -- doesn't update the ptr
<snip>

What value do you want the switch statement to test? If it's the
pointer *before* it is incremented, then your code is right (well not
really, see below). If you want it to be the pointer *after*
incrementation, then you have to do a pre-increment:

buf[ptr]=(serialinput);
switch (buf[++ptr]) { ... }

Also, the names of the variables leads me to believe the code is wrong
anyway. 'ptr' is a pointer, right? You can't subscript an array (or a
pointer) with a pointer; you subscript it with an integer. If 'ptr'
points to an element in the array (or pointer) 'buf', then the code
should look like this:
*ptr = (serialinput);
switch (*++ptr) { ... }

Sebastian

Jul 28 '08 #3

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

Similar topics

10
10882
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a...
35
8289
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
8
11578
by: pb648174 | last post by:
I have a single update statement that updates the same column multiple times in the same update statement. Basically i have a column that looks like .1.2.3.4. which are id references that need to...
1
3455
by: Chris Johnson | last post by:
Here is my dilema. I have a 120 GB database that I need to mask customer credit card numbers in. The field is a varchar (16). I need to update the field so that we only store the first 4 numbers...
2
19505
by: Ray | last post by:
I have a list of about 20,000 rows that I am updating. I loop through each row in my program and basically do the following (1) select * from TABLE where SID= for update /*lock the row*/ (2)...
5
2013
by: junglist | last post by:
Hi guys, I've been trying to implement an editable datagrid and i have been succesful up to the point where i can update my datagrid row by row. However what used to happen was that once i updated...
4
2024
by: ^MisterJingo^ | last post by:
Hi all, I have a web page which has a single dropdownlist containing 3 items. Below the dropdownlist are two listboxes. Depending on the option selected from the dropdownlist, the left most...
3
2188
by: shark | last post by:
Hi all. i am facing a deadlock problem .i have included the -t1204 and -T3605 trace flags and have got the following o/p pu tin sqls server logs. 2006-06-01 17:49:21.84 spid4 2006-06-01...
2
2869
osward
by: osward | last post by:
Hello there, I am using phpnuke 8.0 to build my website, knowing little on php programing. I am assembling a module for my member which is basically cut and paste existing code section of...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7095
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
7361
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...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5602
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,...
1
5026
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
4693
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
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1523
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.