Solved

[C, C++, g++] Void pointer in arithmetics with warning in Ubuntu

Posted on 2012-03-17
8
838 Views
Last Modified: 2012-03-17
If i compile my code with gcc under Ubuntu i get no warning and everything works good.

If i compile the same code with g++, i get this warning:

warning: pointer of type ‘void *’ used in arithmetic

This is the code:
 ssize_t read_num(int fd, void *data, size_t count) {
	ssize_t total = 0;

	while (count) {
		ssize_t rd = read(fd, data, count);
		if (rd < 0) {
			perror("read");
			return rd;
		} else {
			count -= rd;
			data += rd; <-- PROBLEM IS HERE
			total += rd;
		}
	}
	return total;
}

Open in new window

However, the program compiles and it works fine but i'd like to solve the warning.

I know that i cannot use void pointer in math in c++ because it is not a safe operation, but i do not know how to solve the warning.

Can you help me, please?
My g++ version is 4.5.2
0
Comment
Question by:ichigokurosaki
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 37732924
void has no size.

So, adding to a pointer-to-void is nonsensical, because for doing so, the size of the objects pointed to needs to be known.

Instead of void*, use eg. unsigned char*, or whatever pointer type you need.


The reason it worked with gcc, is because of backward compatibility reasons, which treats void* to be a pointer to byte-size objects. It's a bad idea to depend on this though.
0
 

Author Comment

by:ichigokurosaki
ID: 37732959
The problem is that i have this:

typedef struct {
	unsigned char length;
	unsigned char magic;
	unsigned char type;
} zeemote_hdr_t;

// then i do:

 zeemote_hdr_t hdr;

int rd = read_num(bt, &hdr, sizeof(hdr));

ssize_t read_num(int fd, void *data, size_t count) {
	ssize_t total = 0;

	while (count) {
		ssize_t rd = read(fd, data, count);
		if (rd < 0) {
			perror("read");
			return rd;
		} else {
			count -= rd;
			data += rd;
			total += rd;
		}
	}
	return total;
}

Open in new window


So, i do not know how to properly use the data pointer because if i change the void point in (unsigned char*) or (int *), i always get errors in g++.

What do you suggest me?
0
 

Author Comment

by:ichigokurosaki
ID: 37732976
Do you think it can be ok if i do this:

ssize_t read_num(int fd, void *data, size_t count) {
	ssize_t total = 0;
        char *data_changed = (char*) data;
	while (count) {
		ssize_t rd = read(fd, data_changed, count);
		if (rd < 0) {
			perror("read");
			return rd;
		} else {
			count -= rd;
			data_changed += rd;
			total += rd;
		}
	}
	return total;
}

Open in new window


g++ does not show warnings now.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Infinity08
ID: 37733026
>> Do you think it can be ok if i do this:

In most cases, that will work.

But if you are writing code in C, why use a C++ compiler ?
Or, alternatively, if you are writing C++, why not do it the C++ way, and use streams ?
0
 

Author Comment

by:ichigokurosaki
ID: 37733068
The problem is that i'm using some C functions in a C++ programs.
C functions involve a lot of code lines..
do you think is it better to traslate all of them in C++ by using streams?

They are working fine at the moment and g++ gives me no errors or warnings..
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 37733099
That depends on what you want :)

If you are happy with the way it is now, and all is running fine, then there's no need to change it.
0
 

Author Closing Comment

by:ichigokurosaki
ID: 37733106
Thanks a lot for the suggestion!
0
 

Author Comment

by:ichigokurosaki
ID: 37733107
Yep, for the moment, it seems to be fine :D
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Linux "time" command output redirection 16 218
FMX TCameraComponent Problem 2 92
gdb doesn't stop on breakpoint 2 113
If a cell in a range equals "YES" return specific Test. 4 42
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question