Avatar of ichigokurosaki
ichigokurosaki
Flag for Afghanistan asked on

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

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
CC++Linux OS Dev

Avatar of undefined
Last Comment
ichigokurosaki

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Infinity08

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ichigokurosaki

ASKER
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?
ichigokurosaki

ASKER
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.
Infinity08

>> 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 ?
Your help has saved me hundreds of hours of internet surfing.
fblack61
ichigokurosaki

ASKER
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..
Infinity08

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.
ichigokurosaki

ASKER
Thanks a lot for the suggestion!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ichigokurosaki

ASKER
Yep, for the moment, it seems to be fine :D