473,516 Members | 3,064 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Purpose of using a void cast char* as a statement

7 New Member
I have some legacy code in a project I'm working. We have a line of code that looks like the following:

Expand|Select|Wrap|Line Numbers
  1. int init_tbl (char *tblname) {
  2.     int ret = -1;
  3.  
  4.     /* some more code ... */
  5.  
  6.     setBatchSize(getBatchSize());
  7.  
  8.     (void) tblname;
  9.  
  10.     return ( ret );
  11. }
  12.  
What is the purpose of the line:
Expand|Select|Wrap|Line Numbers
  1. (void) tblname;
Jun 10 '15 #1
2 1287
donbock
2,426 Recognized Expert Top Contributor
That line does nothing; and yet, it serves a purpose. Presumably, argument tblname is not otherwise accessed within the function. Although not required to do so, some compilers (or static checkers like lint) will generate a warning if a function argument is not used. The line in question fools the compiler into thinking tblname is used, so the warning is not generated.

I would classify this as tricky code that ought to be well commented. Some static checkers allow you to embed directives in comments that tune what generates an warning. A directive in a comment is better because it is obvious that comments have no effect on program execution.
Jun 10 '15 #2
donbock
2,426 Recognized Expert Top Contributor
Strictly speaking that line directs the compiler to read the current value of tblname and then to throw the value away. The compiler may well optimize out this access to tblname.

Behavior is similar in the following code snippet, but the purpose is different.
Expand|Select|Wrap|Line Numbers
  1. void function(volatile char *p) {
  2.     (void) *p;
  3. }
In this case, the value at the location referenced by p is read and then discarded. However, the volatile keyword prevents this access from being optimized out. There are real situations where a memory-mapped I/O port needs to be read from, but the value you get out of it is irrelevant.
Jun 10 '15 #3

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

Similar topics

11
55479
by: Pontus F | last post by:
Hi I am learning C++ and I'm still trying to get a grip of pointers and other C/C++ concepts. I would appreciate if somebody could explain what's wrong with this code: ---begin code block--- #include "stdio.h" #include "string.h" void printText(char c){
7
11163
by: James Purser | last post by:
I have a piece of c code I am trying to include in a c++/wxWidgets project. len = strlen(sha1_dir); path = malloc(len + 40); memcpy(path, sha1_dir, len); When I try to compile the above using g++ I get "error: invalid conversion from void* to char*"
11
3612
by: bildad | last post by:
I like fixing things so I'm working on programs from books that came in a package deal with Borland C++ 4.52. I'm trying to fix them to compile with gcc 3.4.2. I don't understand what's causing the following error but I think it might have something to do with malloc(). Any insight appreciated. DAY01E4.C:10: error: invalid conversion from...
1
4443
by: ziroi | last post by:
Hello! I am new here to the forums and still learning C. I understand your policies on coursework, but I have an error that I've run around and around and can't figure out where my problem is with my assignment. I hope you guys can point me in the right direction. I have a program which has an array of linked lists, with specific struct...
48
16837
by: Francine.Neary | last post by:
I've been reflecting on these two types of pointer. As far as I can glean from books, void * and char * are functionally equivalent: the key property of both is that they are pointers that can be faithfully cast to any other pointer type. The only difference seems to be that it is legal to perform arithmetic on a char *, but not on a void...
5
3531
by: ekin | last post by:
hello everyone.I am new here,and also new to c;I got new problem #include <stdio.h> #include <conio.h> void ungets(char *s) { while (*s != '\0') ungetc(*s++,stdin); ungetc('\0',stdin);
1
4229
by: alex | last post by:
The following function is quoted from Judy source code: // **************************************************************************** // J U D Y F R E E void JudyFree( void * PWord, Word_t Words) {
3
2611
by: ANURAGBAJPAI | last post by:
Please tell me what is the purpose of void in function main.
2
8404
Lewis Mbuthia
by: Lewis Mbuthia | last post by:
I tried to write a program to write a wav file.Could you please debug it for me? #include <math.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #include <unistd.h>
0
7276
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7182
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7408
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7142
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5714
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5110
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3267
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
825
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.