473,657 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

incompatible pointer type

code segment:

long int * size;
char entry[4][16];
.............
size=&entry[row][col];

*************** *************** *******
Gcc reported 'assignment of incompatible pointer type'.

MY QUESTION IS:
I understand that pointer is just a memory unit that contains the
address(or starting address)of other data object(i.e. int, float,
array, struct, etc.). So it is just an address. How does C implement
the pointer underlying? Why there can be different pointer types. If
so, what is the difference between an integer pointer type and a float
pointer type, for example?

Thank you!

Ji

Mar 7 '07 #1
13 11984

"william" <wi**********@g mail.comwrote in message
news:11******** **************@ c51g2000cwc.goo glegroups.com.. .
code segment:

long int * size;
char entry[4][16];
............
size=&entry[row][col];

*************** *************** *******
Gcc reported 'assignment of incompatible pointer type'.
The report is correct.
You should change 'size' to type 'char *'
>
MY QUESTION IS:
I understand that pointer is just a memory unit that contains the
address(or starting address)of other data object(i.e. int, float,
array, struct, etc.). So it is just an address. How does C implement
the pointer underlying?
That's specific to the implementation.
>Why there can be different pointer types.
Because the language defines them as different.
If
so, what is the difference between an integer pointer type and a float
pointer type, for example?
The difference is that they're two different types.

-Mike
Mar 7 '07 #2
"william" <wi**********@g mail.comwrote in message
news:11******** **************@ c51g2000cwc.goo glegroups.com.. .
code segment:

long int * size;
char entry[4][16];
............
size=&entry[row][col];

*************** *************** *******
Gcc reported 'assignment of incompatible pointer type'.

MY QUESTION IS:
I understand that pointer is just a memory unit that contains the
address(or starting address)of other data object(i.e. int, float,
array, struct, etc.). So it is just an address. How does C implement
the pointer underlying? Why there can be different pointer types. If
so, what is the difference between an integer pointer type and a float
pointer type, for example?

Thank you!

Ji
And int pointer type and a float pointer type point to different types of
data. int* points to data that contains an integer. float* points to data
that contains a float. There may be some circumstances where you may wish
to convert between pointer types, but you better know what you're doing.

In your example, you are making size a pointer to an int, but then trying to
get it to point to a character. The compiler rightfully says, wait a second
buddy, those are two different types. If you really wish to do this, the
you could use reinterpret_cas t to change the type of the pointer. It would
compile but depending on hwo you are use it could cause all kinds of
problems at run time. I.E.

size = reinterpret_cas t<int*>( &entry[row][col] );

Again, this is very dangerous. Especially since a char is only 1 character
and an interger is more (4 on my system, 8 on some, who knows how many on
others).
Mar 7 '07 #3
S S
On Mar 7, 8:13 pm, "william" <william.m...@g mail.comwrote:
code segment:

long int * size;
char entry[4][16];
............
size=&entry[row][col];

*************** *************** *******
Gcc reported 'assignment of incompatible pointer type'.

MY QUESTION IS:
I understand that pointer is just a memory unit that contains the
address(or starting address)of other data object(i.e. int, float,
array, struct, etc.). So it is just an address. How does C implement
the pointer underlying? Why there can be different pointer types. If
so, what is the difference between an integer pointer type and a float
pointer type, for example?

Thank you!

Ji
Say, how would you increment the pointer then? You need to do several
operations on pointer and if compiler does not know what type it is,
you cannt do even p++. As simple as that.

Mar 7 '07 #4
On Mar 7, 11:32 am, "S S" <sarvesh.si...@ gmail.comwrote:
On Mar 7, 8:13 pm, "william" <william.m...@g mail.comwrote:
code segment:
long int * size;
char entry[4][16];
............
size=&entry[row][col];
*************** *************** *******
Gcc reported 'assignment of incompatible pointer type'.
MY QUESTION IS:
I understand that pointer is just a memory unit that contains the
address(or starting address)of other data object(i.e. int, float,
array, struct, etc.). So it is just an address. How does C implement
the pointer underlying? Why there can be different pointer types. If
so, what is the difference between an integer pointer type and a float
pointer type, for example?
Thank you!
Ji

Say, how would you increment the pointer then? You need to do several
operations on pointer and if compiler does not know what type it is,
you cannt do even p++. As simple as that.
I understand it. It makes a lot of sense. Pointer++ :-)

Mar 7 '07 #5
On Mar 7, 3:12 pm, "william" <william.m...@g mail.comwrote:
On Mar 7, 11:32 am, "S S" <sarvesh.si...@ gmail.comwrote:
On Mar 7, 8:13 pm, "william" <william.m...@g mail.comwrote:
code segment:
long int * size;
char entry[4][16];
............
size=&entry[row][col];
*************** *************** *******
Gcc reported 'assignment of incompatible pointer type'.
MY QUESTION IS:
I understand that pointer is just a memory unit that contains the
address(or starting address)of other data object(i.e. int, float,
array, struct, etc.). So it is just an address. How does C implement
the pointer underlying? Why there can be different pointer types. If
so, what is the difference between an integer pointer type and a float
pointer type, for example?
Thank you!
Ji
Say, how would you increment the pointer then? You need to do several
operations on pointer and if compiler does not know what type it is,
you cannt do even p++. As simple as that.

I understand it. It makes a lot of sense. Pointer++ :-)
*************** *************** ****
My original intention was to use any bytes to comprise an int. So I
used two ways to do that:

1.the function atoi():

However, I still get some problems here:
char c[]="ABCD";
int i;

i=atoi(&c[0]);// I expected to get 65: A's ascii value

the result of the line of code above is ZERO, which confuse me a lot.

2.conversion:

i=(int)c[0];

this case it worked. However, if c[0]is arbitery. 'i' always get
negative value, how come?

Thanks
Mar 7 '07 #6
On Mar 7, 2:19 pm, "william" <william.m...@g mail.comwrote:
My original intention was to use any bytes to comprise an int. So I
used two ways to do that:

1.the function atoi():

However, I still get some problems here:
char c[]="ABCD";
int i;

i=atoi(&c[0]);// I expected to get 65: A's ascii value

the result of the line of code above is ZERO, which confuse me a lot.
I think you are misinterpreting the meaning of atoi().

atoi() assumes that the string is an ASCII representation of an
integer.

char *c = "1234";
i = atoi(c); // i == 1234

When the string is not an ASCII representation of an integer (like
"ABCD"), it returns zero.

char *c = "ABCD";
i = atoi(c); // i == 0
>
2.conversion:

i=(int)c[0];

this case it worked. However, if c[0]is arbitery. 'i' always get
negative value, how come?
What do you mean by arbitrary?

Think of it this way -- the integral types in C/C++ are just that:
integers.
On most 32-bit architectures, a char is one byte, and an int is 4.
It's still a one-byte integer, though.

Mar 7 '07 #7
On Mar 7, 5:01 pm, jlongstr...@gma il.com wrote:
On Mar 7, 2:19 pm, "william" <william.m...@g mail.comwrote:
My original intention was to use any bytes to comprise an int. So I
used two ways to do that:
1.the function atoi():
However, I still get some problems here:
char c[]="ABCD";
int i;
i=atoi(&c[0]);// I expected to get 65: A's ascii value
the result of the line of code above is ZERO, which confuse me a lot.

I think you are misinterpreting the meaning of atoi().

atoi() assumes that the string is an ASCII representation of an
integer.

char *c = "1234";
i = atoi(c); // i == 1234

When the string is not an ASCII representation of an integer (like
"ABCD"), it returns zero.

char *c = "ABCD";
i = atoi(c); // i == 0
2.conversion:
i=(int)c[0];
this case it worked. However, if c[0]is arbitery. 'i' always get
negative value, how come?

What do you mean by arbitrary?
By arbitrary, I mean if c[0] contains any possible valued raning from
0x00~0xff. Then sometimes, (int)any_char returns negative value.

I got a solution now: if I specify "any_char" as an unsigned char, the
results of conversion is always postive.

Again, does any one has better solution to creat an int using 4
individual bytes(or a 4 byte array).

Thanks
Think of it this way -- the integral types in C/C++ are just that:
integers.
On most 32-bit architectures, a char is one byte, and an int is 4.
It's still a one-byte integer, though.

Mar 8 '07 #8
william wrote:
On Mar 7, 5:01 pm, jlongstr...@gma il.com wrote:
>>On Mar 7, 2:19 pm, "william" <william.m...@g mail.comwrote:

>>>My original intention was to use any bytes to comprise an int. So I
used two ways to do that:
>>>1.the function atoi():
>>>However, I still get some problems here:
char c[]="ABCD";
int i;
>>>i=atoi(&c[0]);// I expected to get 65: A's ascii value
>>>the result of the line of code above is ZERO, which confuse me a lot.

I think you are misinterpreting the meaning of atoi().

atoi() assumes that the string is an ASCII representation of an
integer.

char *c = "1234";
i = atoi(c); // i == 1234

When the string is not an ASCII representation of an integer (like
"ABCD"), it returns zero.

char *c = "ABCD";
i = atoi(c); // i == 0

>>>2.conversion :
>>>i=(int)c[0];
>>>this case it worked. However, if c[0]is arbitery. 'i' always get
negative value, how come?

What do you mean by arbitrary?

By arbitrary, I mean if c[0] contains any possible valued raning from
0x00~0xff. Then sometimes, (int)any_char returns negative value.

I got a solution now: if I specify "any_char" as an unsigned char, the
results of conversion is always postive.

Again, does any one has better solution to creat an int using 4
individual bytes(or a 4 byte array).

Thanks
>>Think of it this way -- the integral types in C/C++ are just that:
integers.
On most 32-bit architectures, a char is one byte, and an int is 4.
It's still a one-byte integer, though.


A union is one possiblilty

union S
{
char a[4];
int i;
};

S s;
s.a[0] = 'A';
s.a[1] = 'B';
s.a[2] = 'C';
s.a[3] = 'D';
cout << s.i;

Of course the are all sorts of platform dependencies here, this is not
portable code.

john
Mar 8 '07 #9
>
A union is one possiblilty

union S
{
char a[4];
int i;
};

S s;
s.a[0] = 'A';
s.a[1] = 'B';
s.a[2] = 'C';
s.a[3] = 'D';
cout << s.i;

Of course the are all sorts of platform dependencies here, this is not
portable code.

john
BTW i'm not trying to suggest the any sort of conversion from hex is
going on here (not sure if that is what you want or not).

john
Mar 8 '07 #10

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

Similar topics

3
17764
by: Brian Stubblefield | last post by:
Dear clc members, I am rather new to the C programming language. I have a rather large program that I am currently debugging. Currently, the following snippet of code in the c program: char skillkey; char (*fillkey_ptr)= skillkey;
15
9339
by: Chris Readle | last post by:
Hi all, Somewhat new to C and I'm getting the following error from my latest code. Here's the warning I'm getting: chris_readle_project3_assignment3.c: In function `main': chris_readle_project3_assignment3.c:23: warning: passing arg 1 of `displaySales' from incompatible pointer type And here is the code in question:
1
2864
by: Josh Wilson | last post by:
Hey gang, So I have my stdin which is user defined file, and am wanting to write it to a temporary file (seems odd I know, but there is a sincere reason), and then use that file as the stdin for the remaining part of the program. I have combed the FAQ and got some preliminary questions answered, and tried to model this after what I have seen suggested to people previously, but for my strcpy and freopen I get, "warning: passing arg 1 of...
6
5258
by: PraZ | last post by:
Hi all. Here is a simple code, which when compiled with gcc results in the warning "incompatible pointer type" for arg 1, as expected. But this is just what I want to do, because it makes it easy for me to handle the single dimensional stream I have as a multidimensional array inside the function func(). Now, I am just wondering if there is a way by which I can disable this incompatible pointer type warning in gcc. Any suggestions?
10
4106
by: gk245 | last post by:
I have something like this: #include <stdio.h> main () { struct line { char write; char read;
8
3262
by: Michael | last post by:
Hi all, why do I get a message: warning: passing arg 1 of `collectInput' from incompatible pointer type In 'main' have: char inputString; collectInput(&inputString);
8
24221
by: fei.liu | last post by:
I have the following source code. It seems wierd to me why gca's value cannot be reassigned. It's afterall a pointer and has a pointer value. I am aware that the standard says it's not allowed. But I cannot convince myself why this is prohibited. static int ia = {64, 64, 64, 64}; static char ca = {'a', 'b', 'c', 'd'}; static int iaa = {64, 64, 64, 64}; static char caa = {'a', 'b', 'c', 'd'}; static char cab = "abcd";
1
1519
by: wanglei0214 | last post by:
I compiles a program in SLOS, but there is a warning i donot know how to remove? here is the framework of the code: typedef struct device_tree { ...... union {
1
2866
by: pyo2004 | last post by:
Here's the definition of work_struct struct work_struct { unsigned long pending; struct list_head entry; void (*func)(void *); void *data; void *wq_data; struct timer_list timer; };
0
8394
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8503
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7327
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6164
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4152
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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 we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.