473,414 Members | 1,777 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,414 software developers and data experts.

Pointers pointers.

I thought I could assign an array of chars to a pointer to pointer.
However, when I do
the following:

#include <stdio.h>

int main(void) {
char buff[] ="test";
char **output = &buff;

return 0;
}

I get:
$gcc -g ptr.c -o ptr
ptr.c: In function `main':
ptr.c:5: warning: initialization from incompatible pointer type

What am I doing wrong here?

Chad

Sep 8 '06 #1
16 1850
Chad wrote:
I thought I could assign an array of chars to a pointer to pointer.
However, when I do
the following:

#include <stdio.h>

int main(void) {
char buff[] ="test";
char **output = &buff;

return 0;
}

I get:
$gcc -g ptr.c -o ptr
ptr.c: In function `main':
ptr.c:5: warning: initialization from incompatible pointer type
The address of a char[5] is not a char **, it is a char (*)[5];
char (*output)[5] = &buff;
Sep 8 '06 #2

Nils O. Selåsdal wrote:
Chad wrote:
I thought I could assign an array of chars to a pointer to pointer.
However, when I do
the following:

#include <stdio.h>

int main(void) {
char buff[] ="test";
char **output = &buff;

return 0;
}

I get:
$gcc -g ptr.c -o ptr
ptr.c: In function `main':
ptr.c:5: warning: initialization from incompatible pointer type

The address of a char[5] is not a char **, it is a char (*)[5];
char (*output)[5] = &buff;
Perhaps this might be going beyond the limits of decent programming,
but would something like the following work:

#include <stdio.h>
#include <string.h>

int main(void) {
char buff[] ="test";

size_t len = strlen(buff) + 1;

char (*output)[len]=&buff;

return 0;
}

Sep 8 '06 #3
Chad wrote:
Nils O. Selåsdal wrote:
Chad wrote:
I thought I could assign an array of chars to a pointer to pointer.
However, when I do
the following:
>
#include <stdio.h>
>
int main(void) {
char buff[] ="test";
char **output = &buff;
>
return 0;
}
>
I get:
$gcc -g ptr.c -o ptr
ptr.c: In function `main':
ptr.c:5: warning: initialization from incompatible pointer type
The address of a char[5] is not a char **, it is a char (*)[5];
char (*output)[5] = &buff;

Perhaps this might be going beyond the limits of decent programming,
but would something like the following work:

#include <stdio.h>
#include <string.h>

int main(void) {
char buff[] ="test";

size_t len = strlen(buff) + 1;

char (*output)[len]=&buff;
char (*output)[sizeof buff]=&buff;
return 0;
}
#include <stdio.h>

int main(void) {
char buff[] = "test";
char *p = buff;

printf("%s\n", p);
return 0;
}

Sep 8 '06 #4
"Chad" <cd*****@gmail.comwrites:
I thought I could assign an array of chars to a pointer to pointer.
However, when I do
the following:

#include <stdio.h>

int main(void) {
char buff[] ="test";
char **output = &buff;

return 0;
}

I get:
$gcc -g ptr.c -o ptr
ptr.c: In function `main':
ptr.c:5: warning: initialization from incompatible pointer type

What am I doing wrong here?
The comp.lang.c FAQ is at <http://www.c-faq.com/>. Read section 6,
"Arrays and Pointers".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 8 '06 #5

Keith Thompson wrote:
"Chad" <cd*****@gmail.comwrites:
I thought I could assign an array of chars to a pointer to pointer.
However, when I do
the following:

#include <stdio.h>

int main(void) {
char buff[] ="test";
char **output = &buff;

return 0;
}

I get:
$gcc -g ptr.c -o ptr
ptr.c: In function `main':
ptr.c:5: warning: initialization from incompatible pointer type

What am I doing wrong here?

The comp.lang.c FAQ is at <http://www.c-faq.com/>. Read section 6,
"Arrays and Pointers".
Hello Keith. The faq says:

Given an array a and pointer p, <snipIf you were to assign the
array's address to the pointer:
p = a;
then p[3] and a[3] would access the same element.

If I write code as follows, is it right?

char buff[] = "hello world";
char *p = buff;

Is a cast needed on the second line of code, i.e., should I write it as
follows:

char *p = (char *) buff;

Thank you.

Sep 9 '06 #6
lovecreatesbea...@gmail.com posted:
Is a cast needed on the second line of code, i.e., should I write it as
follows:

char *p = (char *) buff;

No. The rule of thumb is:

Only use a cast if the code won't compile without one.

There are exceptions to this rule of course, such as using casts to suppress
compiler warnings:

int Func(long const val)
{
return (int)(val / 89);
}

--

Frederick Gotham
Sep 9 '06 #7
lovecreatesbea...@gmail.com wrote:
Keith Thompson wrote:

The comp.lang.c FAQ is at <http://www.c-faq.com/>. Read section 6,
"Arrays and Pointers".

Hello Keith. The faq says:

Given an array a and pointer p, <snipIf you were to assign the
array's address to the pointer:
p = a;
then p[3] and a[3] would access the same element.

If I write code as follows, is it right?

char buff[] = "hello world";
char *p = buff;

Is a cast needed on the second line of code, i.e., should I write it as
follows:

char *p = (char *) buff;
In K&R2, sec 5.3, it writes the code like this without a cast. It seems
that the following code is right:

char *p = buff;
or
p = buff;

Is there a legal implicit conversion?

Sep 9 '06 #8
"Frederick Gotham" writes:
No. The rule of thumb is:

Only use a cast if the code won't compile without one.
Sounds like a bad rule of thumb to me.

Consider:

double mean;
int sum;
int n;
.....
mean = sum/n;

Also casts can contribute to the documentation for people who must
subsequently diddle with the code.
Sep 9 '06 #9

lovecreatesbea...@gmail.com wrote:
lovecreatesbea...@gmail.com wrote:
Keith Thompson wrote:
>
The comp.lang.c FAQ is at <http://www.c-faq.com/>. Read section 6,
"Arrays and Pointers".
Hello Keith. The faq says:

Given an array a and pointer p, <snipIf you were to assign the
array's address to the pointer:
p = a;
then p[3] and a[3] would access the same element.

If I write code as follows, is it right?

char buff[] = "hello world";
char *p = buff;

Is a cast needed on the second line of code, i.e., should I write it as
follows:

char *p = (char *) buff;

In K&R2, sec 5.3, it writes the code like this without a cast. It seems
that the following code is right:

char *p = buff;
or
p = buff;

Is there a legal implicit conversion?
I understand it now. The array name in expression is a pointer to the
first element of the array. There is no implicit conversion and no need
to add a cast.

Sep 9 '06 #10
"osmium" <r1********@comcast.netwrites:
"Frederick Gotham" writes:
>No. The rule of thumb is:

Only use a cast if the code won't compile without one.

Sounds like a bad rule of thumb to me.
A rule of thumb, by definition, is not universal. The stated rule of
thumb is a very good one.
Consider:

double mean;
int sum;
int n;
....
mean = sum/n;
Yes, that's a case where a cast would be appropriate.
Also casts can contribute to the documentation for people who must
subsequently diddle with the code.
In such cases, it's usually better to leave the cast out and depend on
anyone reading the code to understand how C works.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 9 '06 #11
Keith Thompson said:
"osmium" <r1********@comcast.netwrites:
>"Frederick Gotham" writes:
>>No. The rule of thumb is:

Only use a cast if the code won't compile without one.

Sounds like a bad rule of thumb to me.

A rule of thumb, by definition, is not universal. The stated rule of
thumb is a very good one.
I beg to differ. There are very few situations where a cast is a good idea,
but IIRC in most of those very few situations, omitting the cast will not
necessarily lead to a diagnostic message being issued. For example, to*,
is*, and the printfing of pointers.
>
>Consider:

double mean;
int sum;
int n;
....
mean = sum/n;

Yes, that's a case where a cast would be appropriate.
<shrug>
mean = sum;
mean /= n;
</shrug>

Gain a cast to lose a line. Swings and roundabouts.
>Also casts can contribute to the documentation for people who must
subsequently diddle with the code.

In such cases, it's usually better to leave the cast out and depend on
anyone reading the code to understand how C works.
Right. Trust the programmer, despite your instincts. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 9 '06 #12
osmium wrote:
"Frederick Gotham" writes:
>No. The rule of thumb is:

Only use a cast if the code won't compile without one.

Sounds like a bad rule of thumb to me.

Consider:

double mean;
int sum;
int n;
....
mean = sum/n;
Bad example IMO. The above stores the truncated integral part from
the division, and may well be exactly what is desired. If this is
not desired, there are two possibilities:

a. mean = sum / (double)n;
b. mean = (double)sum / n;

and the point of each is to do the arithmetic in the floating point
processor, rather than the integer processor.

This is one area where Pascal avoids confusion, by having the /
operator for reals, and the DIV operator for integers. One more
argument against overloading of operators.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Sep 9 '06 #13
"CBFalconer" writes:
osmium wrote:
>"Frederick Gotham" writes:
>>No. The rule of thumb is:

Only use a cast if the code won't compile without one.

Sounds like a bad rule of thumb to me.

Consider:

double mean;
int sum;
int n;
....
mean = sum/n;

Bad example IMO. The above stores the truncated integral part from
the division, and may well be exactly what is desired.
You must be much older than me. I have *never* wanted that result. To each
his own.
Sep 9 '06 #14
lovecreatesbea...@gmail.com wrote:
>
lovecreatesbea...@gmail.com wrote:
lovecreatesbea...@gmail.com wrote:
Keith Thompson wrote:

The comp.lang.c FAQ is at <http://www.c-faq.com/>. Read section 6,
"Arrays and Pointers".
>
Hello Keith. The faq says:
>
Given an array a and pointer p, <snipIf you were to assign the
array's address to the pointer:
p = a;
then p[3] and a[3] would access the same element.
>
If I write code as follows, is it right?
>
char buff[] = "hello world";
char *p = buff;
>
Is a cast needed on the second line of code, i.e., should I write it as
follows:
>
char *p = (char *) buff;
In K&R2, sec 5.3, it writes the code like this without a cast. It seems
that the following code is right:

char *p = buff;
or
p = buff;

Is there a legal implicit conversion?

I understand it now. The array name in expression is a pointer to the
first element of the array.
There is no implicit conversion and no need to add a cast.
N869
6.3.2 Other operands
6.3.2.1 Lvalues and function designators

[#3] Except when it is the operand of the sizeof operator or
the unary & operator, or is a string literal used to
initialize an array, an expression that has type ``array of
type'' is converted to an expression with type ``pointer to
type'' that points to the initial element of the array
object and is not an lvalue.

--
pete
Sep 9 '06 #15

Keith Thompson wrote:
"osmium" <r1********@comcast.netwrites:
"Frederick Gotham" writes:
No. The rule of thumb is:

Only use a cast if the code won't compile without one.
Sounds like a bad rule of thumb to me.

A rule of thumb, by definition, is not universal. The stated rule of
thumb is a very good one.
Consider:

double mean;
int sum;
int n;
....
mean = sum/n;

Yes, that's a case where a cast would be appropriate.
Do you mean the casts for "sum" or "n"? It is because wrong data types
were used there. The types of "sum" or "n" should not be integer types,
they should be defined as float or double, etc., I think.

Sep 10 '06 #16
"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrites:
Keith Thompson wrote:
>"osmium" <r1********@comcast.netwrites:
[...]
Consider:

double mean;
int sum;
int n;
....
mean = sum/n;

Yes, that's a case where a cast would be appropriate.

Do you mean the casts for "sum" or "n"? It is because wrong data types
were used there. The types of "sum" or "n" should not be integer types,
they should be defined as float or double, etc., I think.
It depends. If you want the average (as a real value) of a sequence
of integer values, it makes sense for sum and n to be integers, and
mean to be double.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 10 '06 #17

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

Similar topics

27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
3
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
12
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...

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.