473,327 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Pointers and arrays.

#include <string.h>
#include <stdio.h>
#include <windows.h>
int main(int argc, char *argv[])
{

char s[10] = "welcome";

char *p = s;
char **prt = p; //error happens here
return 0;
}
Aug 19 '08 #1
13 1383

"raashid bhatt" <ra**********@gmail.comwrote in message
news:7c**********************************@t1g2000p ra.googlegroups.com...
#include <string.h>
#include <stdio.h>
#include <windows.h>
<windows.his not a standard C header. Please
omit such from code posted here (it has nothing
to do with your question anyway).
>

int main(int argc, char *argv[])
{

char s[10] = "welcome";

char *p = s;
char **prt = p; //error happens here
That's because you're trying to assign
an incompatible type. Write:

char **prt = &p;

return 0;
}

I've shown you correct syntax. But what
exactly are you trying to do?

-Mike
Aug 19 '08 #2
On Aug 19, 6:54*am, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"raashid bhatt" <raashidbh...@gmail.comwrote in message

news:7c**********************************@t1g2000p ra.googlegroups.com...
#include <string.h>
#include <stdio.h>
#include <windows.h>

<windows.his not a standard C header. Please
omit such from code posted here (it has nothing
to do with your question anyway).
int main(int argc, char *argv[])
{
char s[10] = "welcome";
char *p = s;
char **prt = p; //error happens here

That's because you're trying to assign
an incompatible type. *Write:

char **prt = &p;
return 0;
}

I've shown you correct syntax. *But what
exactly are you trying to do?

-Mike
this is wrong man why do i need &p as p consists the address
Aug 19 '08 #3
On 19 Aug, 15:01, raashid bhatt <raashidbh...@gmail.comwrote:
On Aug 19, 6:54*am, "Mike Wahler" <mkwah...@mkwahler.netwrote:


"raashid bhatt" <raashidbh...@gmail.comwrote in message
news:7c**********************************@t1g2000p ra.googlegroups.com...
#include <string.h>
#include <stdio.h>
#include <windows.h>
<windows.his not a standard C header. Please
omit such from code posted here (it has nothing
to do with your question anyway).
int main(int argc, char *argv[])
{
char s[10] = "welcome";
char *p = s;
char **prt = p; //error happens here
That's because you're trying to assign
an incompatible type. *Write:
char **prt = &p;
return 0;
}
I've shown you correct syntax. *But what
exactly are you trying to do?
-Mike

this is wrong man why do i need &p as p consists the address
no it *isn't* wrong. Consider the types, p has type char* (ptr-to-
char)
prt has type char** (ptr-to -ptr-to-char). They are not the same.
But &p is the address of a ptr-to-char so it has type ptr-to-ptr-to-
char.
So that assignment is ok.

Get a good text book. And read it.
--
Nick Keighley


Aug 19 '08 #4
raashid bhatt <ra**********@gmail.comwrote:
#include <string.h>
#include <stdio.h>
#include <windows.h>
Do you realize that this is a non-standard header (and that
rather likely it's not needed)?
int main(int argc, char *argv[])
{
char s[10] = "welcome";
char *p = s;
This works because in this context ('s' is used as a value)
it is automatically converted to a pointer to the first
char of 's'. So you have a char pointer on the left and
n the right hand side of the assignment.
char **prt = p; //error happens here
But here you try to assign the value of a char pointer to a
pointer to pointer to char, so you have different types on
the left and right hand side. Did you try

char **prt = &p;
return 0;
}
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Aug 19 '08 #5
raashid bhatt wrote:
#include <string.h>
#include <stdio.h>
#include <windows.h>
int main(int argc, char *argv[])
{

char s[10] = "welcome";

char *p = s;
char **prt = p; //error happens here
prt is of type char** or "pointer to pointer to char". The
expression 'p' on the RHS of the assignment resolves to a value of type
char* which is not a compatible type for prt. You need to store the
*address* of char* object, which you can derive in this case by
applying the address-of operator to 'p'. Hence:

char **prt = &p;

is correct, as would be:

char **prt = &s;

or

char **prt = &p[5];

or even

char **prt = &p[10];

though the last one can be computed and stored but not deferenced.

<snip>

Aug 19 '08 #6
raashid bhatt wrote:
On Aug 19, 6:54*am, "Mike Wahler" <mkwah...@mkwahler.netwrote:
>"raashid bhatt" <raashidbh...@gmail.comwrote in message

news:7c**********************************@t1g2000p ra.googlegroups.com...
>>
#include <string.h>
#include <stdio.h>
#include <windows.h>

<windows.his not a standard C header. Please
omit such from code posted here (it has nothing
to do with your question anyway).
int main(int argc, char *argv[])
{
char s[10] = "welcome";
char *p = s;
char **prt = p; //error happens here

That's because you're trying to assign
an incompatible type. *Write:

char **prt = &p;
return 0;
}

I've shown you correct syntax. *But what
exactly are you trying to do?
this is wrong man why do i need &p as p consists the address
The expression 'p' evaluates to the *value* of 'p', *not* to it's
address. Consider:

int i = 5;
int j = i;

Now what does the expression on the RHS of the second declaration
evaluate to? The address or the value of 'i'? Why do you think pointers
are any different?

int i = 5;
int *ip = &i;
int **ipp = &ip;

In an expression context 'i' will evaluate to the value stored in the
object that it designates. To initialise ip though we need the address
of i, so we apply the address-of operator to get it. The expression ip
will evaluate to the value that it contains, i.e., the address of the
int object to which it has been set to point to, or the address NULL,
or an indeterminate value. To get the *address* of ip we apply the
address-of operator on it, exactly as was the case for i.

As Eric said elsewhere, reading your textbook might be a better approach
than asking multitudes of elementary questions. At the very least go
over the CLC FAQ whose link I gave you earlier.

Aug 19 '08 #7
"raashid bhatt" <ra**********@gmail.comwrote in message
news:ac**********************************@z6g2000p re.googlegroups.com...
On Aug 19, 6:54 am, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"raashid bhatt" <raashidbh...@gmail.comwrote in message

news:7c**********************************@t1g2000p ra.googlegroups.com...
char s[10] = "welcome";
char *p = s;
char **prt = p; //error happens here

That's because you're trying to assign
an incompatible type. Write:

char **prt = &p;
return 0;
}

I've shown you correct syntax. But what
exactly are you trying to do?

-Mike
this is wrong man why do i need &p as p consists the address
What I wrote is not wrong, it's correct. My assertion is
supported by Kernighan & Ritchie (inventors of the C language),
The international standard for the C language, and 20 years of
experience using the C language.

What supports your claim that what I wrote is wrong?

-Mike
Aug 19 '08 #8
On Aug 19, 8:57*am, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"raashid bhatt" <raashidbh...@gmail.comwrote in message

news:ac**********************************@z6g2000p re.googlegroups.com...
On Aug 19, 6:54 am, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"raashid bhatt" <raashidbh...@gmail.comwrote in message
news:7c**********************************@t1g2000p ra.googlegroups.com...
char s[10] = "welcome";
char *p = s;
char **prt = p; //error happens here
That's because you're trying to assign
an incompatible type. Write:
char **prt = &p;
return 0;
}
I've shown you correct syntax. But what
exactly are you trying to do?
-Mike
this is wrong man why do i need &p as p consists the address

What I wrote is not wrong, it's correct. *My assertion is
supported by Kernighan & Ritchie (inventors of the C language),
The international standard for the C language, and 20 years of
experience using the C language.

What supports your claim that what I wrote is wrong?

-Mike
This is wrong man correct ur thing
Aug 19 '08 #9
raashid bhatt wrote:
On Aug 19, 8:57 am, "Mike Wahler" <mkwah...@mkwahler.netwrote:
>"raashid bhatt" <raashidbh...@gmail.comwrote in message

news:ac**********************************@z6g2000 pre.googlegroups.com...
On Aug 19, 6:54 am, "Mike Wahler" <mkwah...@mkwahler.netwrote:
>>"raashid bhatt" <raashidbh...@gmail.comwrote in message
>>news:7c**********************************@t1g200 0pra.googlegroups.com...
char s[10] = "welcome";
>>>>char *p = s;
char **prt = p; //error happens here
>>>That's because you're trying to assign
an incompatible type. Write:
>>>char **prt = &p;
>>>>return 0;
}
>>>I've shown you correct syntax. But what
exactly are you trying to do?
>>>-Mike
this is wrong man why do i need &p as p consists the address

What I wrote is not wrong, it's correct. My assertion is
supported by Kernighan & Ritchie (inventors of the C language),
The international standard for the C language, and 20 years of
experience using the C language.

What supports your claim that what I wrote is wrong?

-Mike

This is wrong man correct ur thing
If you know better, why did you ask in the first place?

Bye, Jojo
Aug 19 '08 #10
raashid bhatt wrote:
On Aug 19, 8:57*am, "Mike Wahler" <mkwah...@mkwahler.netwrote:
>"raashid bhatt" <raashidbh...@gmail.comwrote in message

news:ac**********************************@z6g2000p re.googlegroups.com...
>On Aug 19, 6:54 am, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"raashid bhatt" <raashidbh...@gmail.comwrote in message

news:7c**********************************@t1g2000 pra.googlegroups.com...
char s[10] = "welcome";
char *p = s;
char **prt = p; //error happens here
>That's because you're trying to assign
an incompatible type. Write:
>char **prt = &p;
return 0;
}
>I've shown you correct syntax. But what
exactly are you trying to do?
>-Mike
this is wrong man why do i need &p as p consists the address

What I wrote is not wrong, it's correct. *My assertion is
supported by Kernighan & Ritchie (inventors of the C language),
The international standard for the C language, and 20 years of
experience using the C language.

What supports your claim that what I wrote is wrong?

-Mike

This is wrong man correct ur thing
Right. I think we can file this one as another troll.

Aug 19 '08 #11
On 19 Aug, 17:05, raashid bhatt <raashidbh...@gmail.comwrote:
On Aug 19, 8:57*am, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"raashid bhatt" <raashidbh...@gmail.comwrote in message
news:ac**********************************@z6g2000p re.googlegroups.com...
On Aug 19, 6:54 am, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"raashid bhatt" <raashidbh...@gmail.comwrote in message
>news:7c**********************************@t1g2000 pra.googlegroups.com....
char s[10] = "welcome";
char *p = s;
char **prt = p; //error happens here
>That's because you're trying to assign
>an incompatible type. Write:
>char **prt = &p;
return 0;
}
>I've shown you correct syntax. But what
>exactly are you trying to do?
this is wrong man why do i need &p as p consists the address
What I wrote is not wrong, it's correct. *My assertion is
supported by Kernighan & Ritchie (inventors of the C language),
The international standard for the C language, and 20 years of
experience using the C language.
What supports your claim that what I wrote is wrong?

This is wrong man correct ur thing
what does that mean? Write in english.

plonk

--
Nick Keighley
Aug 20 '08 #12
On Aug 19, 11:05 am, raashid bhatt <raashidbh...@gmail.comwrote:
On Aug 19, 8:57 am, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"raashid bhatt" <raashidbh...@gmail.comwrote in message
news:ac**********************************@z6g2000p re.googlegroups.com...
On Aug 19, 6:54 am, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"raashid bhatt" <raashidbh...@gmail.comwrote in message
>news:7c**********************************@t1g2000 pra.googlegroups.com...
char s[10] = "welcome";
char *p = s;
char **prt = p; //error happens here
>That's because you're trying to assign
>an incompatible type. Write:
>char **prt = &p;
return 0;
}
>I've shown you correct syntax. But what
>exactly are you trying to do?
>-Mike
this is wrong man why do i need &p as p consists the address
What I wrote is not wrong, it's correct. My assertion is
supported by Kernighan & Ritchie (inventors of the C language),
The international standard for the C language, and 20 years of
experience using the C language.
What supports your claim that what I wrote is wrong?
-Mike

This is wrong man correct ur thing
It is NOT wrong; check your handy C reference manual or the FAQ. When
doing an assignment, the *types* of both the source and the target
have to be compatible. The *type* of p is char*, which is not the
same as the type of prt, which is char**.

prt is looking for a pointer-to-pointer-to-char. p is simply pointer-
to-char. However, &p evaluates to pointer-to-pointer-to-char. So the
answer, as several people have already pointed out, is to write

char **prt = &p;

If you still think this is wrong, then give up on programming in C;
you're just wasting your time otherwise.
Aug 20 '08 #13
On Tue, 19 Aug 2008 09:05:38 -0700, raashid bhatt <ra**********@gmail.com>
wrote:
On Aug 19, 8:57Â*am, "Mike Wahler" <mkwah...@mkwahler.netwrote:
>"raashid bhatt" <raashidbh...@gmail.comwrote in message

news:ac09bd76-
ae*************************@z6g2000p...legroup s.com...
>On Aug 19, 6:54 am, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"raashid bhatt" <raashidbh...@gmail.comwrote in message
>news:7c9c5378-
ff*************************@t1g2000p...legroup s.com...
char s[10] = "welcome";
char *p = s;
char **prt = p; //error happens here
>That's because you're trying to assign an incompatible type. Write:
>char **prt = &p;
(...)
this is wrong man why do i need &p as p consists the address
Did you read Santosh's response to this question?

>What I wrote is not wrong, it's correct. Â*My assertion is supported by
Kernighan & Ritchie (inventors of the C language), The international
standard for the C language, and 20 years of experience using the C
language.

What supports your claim that what I wrote is wrong?

-Mike

This is wrong man correct ur thing
And I suppose the compiler is wrong as well? Did you actually read
Mike's question?

- Anand
Aug 21 '08 #14

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

Similar topics

19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
11
by: Linny | last post by:
Hi, I need some help in declaring an array of pointers to array of a certain fixed size. I want the pointers to point to arrays of fixed size only (should not work for variable sized arrays of the...
79
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
6
by: Carl-Olof Almbladh | last post by:
Already in the 1st edition of the "White book", Kerigham and Ritchie states the "C is a general purpose language". However, without what is usually called "assumed size arrays" and built-in...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
36
by: raphfrk | last post by:
I have the following code: char buf; printf("%lp\n", buf); printf("%lp\n", &buf); printf("%lp\n", buf); printf("%lp\n", buf); printf("%d\n", buf-buf);
14
by: code break | last post by:
what is the difference in this pointers decalarition ? int *ptr; and int (*ptr);
6
by: joelperr | last post by:
Hello, I am attempting to separate a two dimensional array into two one-dimensional arrays through a function. The goal of this is that, from the rest of the program, a data file consisting of...
8
by: Piotrek | last post by:
Hi, Like almost all of beginners I have problem understanding pointers. Please, look at this piece of code, and please explain me why myswap function doesn't work as it's supposed to do, whereas...
0
by: David Thompson | last post by:
On Wed, 09 Apr 2008 12:34:43 +0500, arnuld <arnVuld@ippiVmail.com> wrote: I think you've got the idea, but: - I would be careful about using 'equal'. Pointers and arrays are different things,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.