Re[2]: Question about double pointers assignment

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



this is same reason as previos example

Can you try to change your code as below?

case INT:

#pint = value;

pint = *value;

for(index0=0;index0<5;index0++)

for(index1=0;index1<5;index1++)

printf("%d ",pint[index0][index1]);

break;

case FLOAT:

pfloat=*value;

#pfloat = value;

for(index0=0;index0<5;index0++)

for(index1=0;index1<5;index1++)

printf("%f ",pfloat[index0][index1]);

break;

Thursday, December 27, 2007, 11:37:35 AM, you wrote:


>

Dear all :

I really appreciate all your VERY kind help.

 

Now I have another problem.

Below is my source code.

 

#include<stdio.h>

#include<stdlib.h>

 

#define INT 0

#define FLOAT 1

void show(void **value,int type)

{

int index0, index1;

int (*pint)[5];

float (*pfloat)[5];

 

switch(type){

case INT:

pint = value;

for(index0=0;index0<5;index0++)

for(index1=0;index1<5;index1++)

printf("%d ",pint[index0][index1]);

break;

case FLOAT:

pfloat = value;

for(index0=0;index0<5;index0++)

for(index1=0;index1<5;index1++)

printf("%f ",pfloat[index0][index1]);

break;

}

}

 

int main(void){

 

int temp0[5][5];

float temp1[5][5];

show((void **)temp0,INT);

show((void **)temp1,FLOAT);

return 0;

}

 

 

I just want to pass different array to the same show function and let it decide the right format to print.

The compilation passes and I can successfully get what I want.

But I have some questions:

1. How can I eliminate the warning message below?

cast.c:14: warning: assignment from incompatible pointer type

I know it comes from I cast (void**) to (int*)[5], a pointer points to a 5 elements integer array.

And I should assign it by casting rather pfloat=value;

But what is the exact type I should use?

pfloat = ((float *)[5])value; //the compiler says, expected _expression_ before “[“ token

 

I just want to know is there any possibilities to get rid of the warning message?

2. if I can get rid of the warning message, does that mean I don’t need the temporary pointer I declare in show, such as int (*pint)[5] and float (*pfloat)[5], and just cast it while printing.

 

 

Appreciate your help,

Cckuo


From: Thippeswamy, Aravind [mailto:AravindT@xxxxxxx] 

Sent: Wednesday, December 26, 2007 4:02 PM

To: Saquib Imam; sahlot arvind; kernelnewbies@xxxxxxxxxxxx

Cc: Cihan Kömeçoglu; C C Kuo(郭哲君); kernelnewbies@xxxxxxxxxxxx

Subject: RE: Question about double pointers assignment

 

int *temp [5]  -> is an array of integer pointers.

            temp++ here would be invalid. Can’t increment an lvalue!

            Here temp is an array i.e. a pointer pointing to a set of integer pointers. Temp+1 here will give an address = temp + sizeof (int *);

 

int (*temp) [5] -> pointer to an array of integers (5 to be precise)

            temp++ is OK here.

            Here temp is an array i.e. a pointer pointing to a set of integer; the set is taken into consideration as a whole. Temp+1 here will give an address = temp + 5*sizeof (int);

 

            

            The arithmetic on a pointer yields a result depending on what it’s pointing to. For example,  a ++ on a char * will make it increment by 1byte (i.e. sizeof (char)). ++ on an int * will increment it by 2 or 4 bytes depending on the size of the integer on that particular machine. Similarly a pointer to a pointer, which may be pointing to anything from char to int to possibly another pointer, will be incremented by sizeof (pointer).

            Here’s the real thing.

            

            Char **ptr2ptr2char = SOME_ADDRESS1;

Here ptr2ptr2char is just another pointer pointing to some pointer pointing to a char.

            Ptr2ptr2char ++ will make it jump by sizeof (ptr2ptr2char) bytes.

 

            Int ** ptr2ptr2int = SOME_ADRESS2;

            Even in this case, ptr2ptr2int ++ will only make this jump by sizeof (ptr2ptr2int) bytes.

            Since all pointers have the same size, both the cases will yield the same result.

 

            Now to make things a bit clear, if you have two pointers, one to a char and another to an int.

            char *ptr2char = SOME_ADDRESS3;

            int *ptr2int = SOME_ADRESS4;

 

            ptr2char ++ will make it jump by 1 byte as the size of the element that this pointer points to is sizeof (char) = 1 byte.

            ptr2int ++ will make it jump by 4 byte as the size of the element that this pointer points to is sizeof (int) = 4 byte.

 

 

            Hope this doesn’t cause any more confusion. 

 

 

Regards,,

Aravind.

 

"Dovie'andi se tovya sagain"

 -Mat Cauthon (WoT).


From: kernelnewbies-bounce@xxxxxxxxxxxx [mailto:kernelnewbies-bounce@xxxxxxxxxxxx] On Behalf Of Saquib Imam

Sent: Wednesday, December 26, 2007 11:37 AM

To: 'sahlot arvind'

Cc: 'Cihan Kömeçoglu'; C_C_Kuo@xxxxxxxxxxxxxx; kernelnewbies@xxxxxxxxxxxx

Subject: RE: Question about double pointers assignment

 

Can u tel me the difference between

Int *temp[5];

And

Int (*temp)[5];

 

 

Thanks & Regards,

Saquib Imam


From: kernelnewbies-bounce@xxxxxxxxxxxx [mailto:kernelnewbies-bounce@xxxxxxxxxxxx] On Behalf Of sahlot arvind

Sent: Wednesday, December 26, 2007 10:34 AM

To: Saquib Imam

Cc: Cihan Kömeçoğlu; C_C_Kuo@xxxxxxxxxxxxxx; kernelnewbies@xxxxxxxxxxxx

Subject: Re: Question about double pointers assignment

 

>#include<stdio.h>

>#include<stdlib.h>

>int    exterint[5][5];

>int    exterint1[5];

>int main(void){

>int index0, index1;

>int **temp1;

>int *temp2;

>temp2=exterint1; //*****

>temp1 = exterint;  //xxxxxxxxx

>The compiler will warn me that at line marked as xxxxx is "assignment from incompatible pointer type." 

> But "******" doesn't get any warning.


Here exterint1 is a pointer to the first element of one dimensional array, which is nothing but an integer. Thus "exterint1" is nothing but simply a pointer  to an integer. Thats why you can assign it "temp2" which is also a pointer of same type. 

But keeping in mind the way two dimensional arrays are treated, "exterint" is a pointer to an array of integers of size 5. Note that it is not a pointer to pointer to an integer, while "temp1" is a pointer to pointer to integer. Thus when in "temp1 = exterint" the assignment is not correct and hence warning. 

To remove it what you can do it, declare "temp1" as a pointer to an array of integer of size 5. Something like this - 

 

int (*temp1)[5];

temp1 = externint;

 

Hope it helps.

 

Best regards

Arvind Sahlot


 

On 12/26/07, Saquib Imam <saquib.imam@xxxxxxxxx> wrote: 

…

int    exterint[5][5]; 

int    exterint1[5]; 

…

int **temp1;

int *temp2;

….

temp2=exterint1; //***** 

temp1 = exterint;  //xxxxxxxxx    "ERROR GIVING LINE"

 

In the above case exterint1 will be pointing to the first element of the integer array, so it can be assigned to a integer pointer. 

 

So the assignment 

temp2=exterint1;   is correct.

 

While in case of exterint , it will be pointing to the first element to the first row , again its pointing to a integer not a pointer to the integer while temp1 is a pointer to the pointer to an integer. 

 

So the assignment  

 temp1 = exterint   gives an error. 

Thanks & Regards,

Saquib Imam


From: kernelnewbies-bounce@xxxxxxxxxxxx [mailto: kernelnewbies-bounce@xxxxxxxxxxxxOn Behalf Of Cihan Kömeçoglu

Sent: Monday, December 24, 2007 4:26 PM

To: C_C_Kuo@xxxxxxxxxxxxxx

Cc: kernelnewbies@xxxxxxxxxxxx

Subject: Re: Question about double pointers assignment

 

I think , the problem is there

 

temp1 = exterint

 

temp1 is pointer to pointer,not pointer to int but you assigned adress of exterint1 and this array of integer; not array of pointer. 

For example like this give you same warning:

 

int * temp1;

int a;

temp1 = a; 

Warrning:"assignment from incompatible pointer type 

 

This is correct if you do like below

 

int *temp1

int exterint[5];

 

temp1 = exterint;

 

Monday, December 24, 2007, 11:49:37 AM, you wrote:

 

>  

Dear all:

I write a program like below: 

 

#include<stdio.h> 

#include<stdlib.h> 

int    exterint[5][5]; 

int    exterint1[5]; 

int main(void){ 

    int index0, index1; 

    int **temp1; 

    int *temp2; 

    temp2=exterint1; //***** 

    temp1 = exterint;  //xxxxxxxxx 

…..

…..}

 

The compiler will warn me that at line marked as xxxxx is "assignment from incompatible pointer type." 

But "******" doesn't get any warning. 

 

Is there any restriction about assigning multi-layer array or something about pointer I miss? 

 

Appreciate your help, 

cckuo

 

***************** ã€€ Novatek Email Confidentiality Notice ***************** 

The information contained in this electronic communication, and any electronic attachments, is confidential or legally privileged and may be subject to protection under the law. It is intended only for the named recipient above. If you have received this communication in error, or are not the named recipient, please immediately notify the sender via reply email and delete this communication from your computer system, and destroy all copies of this communication along with any attachments. Thank you for your cooperation. 

 

Copyright Novatek 2006-2007 All Rights Reserved  

 

 

 

 

-- -- To unsubscribe from this list: send an email with "unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx Please read the FAQ at http://kernelnewbies.org/FAQ 

 


*****************  Novatek Email Confidentiality Notice *****************

The information contained in this electronic communication, and any electronic attachments, is confidential or legally privileged and may be subject to protection under the law. It is intended only for the named recipient above. If you have received this communication in error, or are not the named recipient, please immediately notify the sender via reply email and delete this communication from your computer system, and destroy all copies of this communication along with any attachments. Thank you for your cooperation.


Copyright Novatek 2006-2007 All Rights Reserved 





-- 

Cihan Kömeçoğlu,

EnderUNIX SDT                           mailto:cihan@xxxxxxxxxxxxx

-- To unsubscribe from this list: send an email with "unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx Please read the FAQ at http://kernelnewbies.org/FAQ

[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux