Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C pointer syntax

Name: Anonymous 2007-11-14 12:42

If I declare an array of arrays like this:

const char** p0 = { "##  ", "##  ", "    ", "    " };

I get these warnings:
type_d.h:13: warning: initialization from incompatible pointer type
type_d.h:13: warning: excess elements in scalar initializer
type_d.h:13: warning: (near initialization for ‘p0’)
type_d.h:13: warning: excess elements in scalar initializer
type_d.h:13: warning: (near initialization for ‘p0’)
type_d.h:13: warning: excess elements in scalar initializer
type_d.h:13: warning: (near initialization for ‘p0’)

but if I alter the syntax to

const char* p0[] = { "##  ", "##  ", "    ", "    " };

the warnings are all gone. what's exactly the difference between using * [] and ** ? I really thought they were just the same, just like * and [] or ** and [][].

If I did it like p0[][] = { ... would it be different at all?

Name: Anonymous 2007-11-14 12:48

* is just a pointer to some data
[] explicitly tells the compiler that you have a pointer to an array of items

Why is this so hard to understand?

Name: Anonymous 2007-11-14 13:41

-w0, thread over.

Name: Anonymous 2007-11-14 13:54

You have simply found a case where there are differences between pointers and arrays. Another example would be the behaviour of the sizeof operator.

Name: Anonymous 2007-11-14 15:00

>>3
Hahaha oh wow. Hope you don't work in any industry branch that releases code I might use one day.

Name: Anonymous 2007-11-15 0:48

>>5
RUBY ON RAILS

Name: Anonymous 2007-11-15 4:43

>>3
Hahaha oh wow

Name: Anonymous 2007-11-15 7:25

>>4
fail
The behavior? what behavior? sizeof always tells the truth.
read http://4-ch.net/code/kareha.pl/1187046689/15-

Name: Anonymous 2007-11-15 7:50

>>8
char a[]="penis", *b="penis";
assert(sizeof a != sizeof b);

sizeof always tells the truth.
I didn't mean to imply otherwise.

Name: Anonymous 2007-11-15 8:27

>>9
which is the same with

sizeof (char[6]) != sizeof (char *)


did you by any chance read that link?

Name: Anonymous 2007-11-15 9:08

>>10
What the hell is your point?

Name: Anonymous 2007-11-15 9:12

>>11
Im curious about yours.

Name: Anonymous 2007-11-15 9:17

>>11,12
lol internet argument

Name: Anonymous 2007-11-15 9:21

>>12
That this:
I really thought they were just the same, just like * and []
..is wrong.

Name: Anonymous 2007-11-15 15:32

>>14
NIGGERS NIGGERS NIGGERS

Name: Anonymous 2007-11-15 15:57

>>11
My other point is a reference.

Name: Anonymous 2007-11-15 16:44

>>3
Hahaha, oh wow. All I will say is, I hope you don't ever work in a Professional Enterprise environment.

Name: Anonymous 2007-11-15 16:54

int (*f(int (*g)(int)))(int);

Name: Anonymous 2007-11-15 19:37

>>16
My other pointer is a reference.

Name: Anonymous 2007-11-16 4:22

My other point makes sense

Name: Anonymous 2007-11-18 11:56

>>1
Just read the declarations. When you declare "const char** p0" you are saying "pointer to a pointer to a const char". Declaring "const char* p0[]" you are saying "array of pointers to const chars".

From my POV: C pointers are NOT related to arrays in any way. The confusion only arises from the (very specific) fact that pointers can be "indexed" like arrays. Other than this, they have nothing to do with each other. Have this in mind when using sizeof -- notice how it behaves differently when used with pointers and arrays.

Exception made for [] arrays as formal parameters since those will act like pointers as far as I know. Also, cocks.

Name: Anonymous 2007-11-18 12:02

>>21
TL;DR: with **, the compiler expects a single initializer of type "pointer to pointer to const char", and with *[] the compiler expects a compound initializer made up of "pointers to const char" because arrays != pointers

Name: Anonymous 2007-11-18 12:32

The confusion only arises from the (very specific) fact that pointers can be "indexed" like arrays.
And the reverse: &array[0] == array (== &array), and array[x] == *(array+x).

C pointers and arrays are very much related. They just differ in their declaration/initialization, and sizeof (and maybe something else, I don't remember right now).

Name: Anonymous 2007-11-18 13:26

>>23
And the reverse: &array[0] == array (== &array), and array[x] == *(array+x).
incorrect, `&array[0]' equals `array' only in value context.
Also, neither `&array[0]' or `array' equal `&array'.

As for array[x] == *(array+x) correct, however certain compilers seem to like *(array+x) more than array[x].

C pointers and arrays are very much related. They just differ in their declaration/initialization, and sizeof (and maybe something else, I don't remember right now).

read that FUQIN thread i linked to you fucking moron
>>8

Name: Anonymous 2007-11-18 13:38

>>24
You're wrong.

Name: Anonymous 2007-11-18 13:38

>>24 here
as for what OP asked, you assign an aggregate type to a scalar type.

Name: Anonymous 2007-11-18 13:40

>>25
i am not, explain yourself motherfucker.

Name: Anonymous 2007-11-18 13:46

incorrect, `&array[0]' equals `array' only in value context.
What do you mean by 'value context'?

Also, neither `&array[0]' or `array' equal `&array'.
Proof?

however certain compilers seem to like *(array+x) more than array[x].
And?

Name: Anonymous 2007-11-18 14:06

>>28
What do you mean by 'value context'?
That these two expressions are equal only in value.

Proof?
learn C moron.
If type is 'type' then &type is 'type *'
type != type *

And?
Just felt like sharing a bit of my huge knowledge. (i am huge therefore my knowledge must be HUGE)
Your mind will blast when you understand why *(array+x) /might/ compile to better code than array[x].
A hint: check how most compilers read the source code.

I am disgusted by the amount of C morons here in /prog/, mainly because C is trivial.

Name: Anonymous 2007-11-18 14:17

That these two expressions are equal only in value.
What other contexts are there for those expressions?

type != type *
They still point to the same memory location.

/might/ compile to better code
Explain.

Name: Anonymous 2007-11-18 16:13

>>30
What other contexts are there for those expressions?
haha, oh wow.
Answer me first these:

The c char sequence 'A'
is it an expression?
If it is, what is it's type?
It's signedness?
It's size?

They still point to the same memory location.
you are now thinking with your assembler.
And no -- they don't have to be equal in value.

Explain
I will simply give some hints: consider how the compiler reads what is given to him, notice that x is an expression that has to be evaluated, understand how the compiler evaluates code, how the compiler generates assembly, and be englightened.

Name: Anonymous 2007-11-18 16:15

>>31
its type
Its signedness
Its size
fixed.

Name: Anonymous 2007-11-18 17:05

Answer me first these:
It's an expression (in my view: anything that can appear on the right-hand side of an assignment operator). I don't want to argue definitions though, I'd prefer you just answer my question.

And no -- they don't have to be equal in value.
Gimme a link or something.

I will simply give some hints:
I still don't understand, and am not willing to spend a lot of time thinking about this, so just tell me what you're getting at.

Name: Anonymous 2007-11-18 18:32

>>33
An expression, simply to put, is anything that can be evaluated to a value.

Gimme a link or something.
Your C book.

I still don't understand, and am not willing to spend a lot of time thinking about this, so just tell me what you're getting at.
I don't care.

Lastly, to answer about the expresion 'A'

type: int
signedness: signed (okay saying signedness here is not correct, i should've asked whether it's a signed or unsigned type)
size: sizeof (int), equal or more than 1.

Anyway, you don't know C. that's all.

Name: Anonymous 2007-11-18 19:30

>>34
So you're unable to substantiate your points from >>24. Thanks.

Name: Anonymous 2009-07-12 6:34

You in in are sandwich in standing

Name: Anonymous 2010-12-17 1:33

Are you GAY?
Are you a NIGGER?
Are you a GAY NIGGER?

If you answered "Yes" to all of the above questions, then GNAA (GAY NIGGER ASSOCIATION OF AMERICA) might be exactly what you've been looking for!

Name: Anonymous 2011-02-04 12:44

Don't change these.
Name: Email:
Entire Thread Thread List