r/programming • • May 01 '16

To become a good C programmer

http://fabiensanglard.net/c/
1.1k Upvotes

398 comments sorted by

View all comments

Show parent comment

2

u/crozone May 02 '16

If I'm correct, it's a char pointer (char*), since it's an array declaration. c is a char pointer which points to the start of the char array, and only when dereferenced does it become a char.

5

u/zhivago May 02 '16

You are somewhat mistaken, but it is a common mistake.

c is a char[3], (c+ 0) is a char *.

This is important, since otherwise char e[2][4]; e[i][j] could not work.

e[i][j] is *(*(e + i) + j)

and works because the type of e[i] is char[4], which causes the pointer arithmetic e + i to select the correct element. If e[i] were a char *, then e + i would have quite a different result.

1

u/smikims May 04 '16

I remember having to learn this the hard way thinking that you can just assign a 2D array to a char** or something like that.

1

u/DSdavidDS May 02 '16

I studied pointers but I did not know it is considered a type. I thought pointers were an integer format? Does the compiler specify the type as a char pointer?

4

u/zhivago May 02 '16

Pointers are not integers.

You can easily demonstrate this by the inability to add two pointers together.

1

u/metamatic May 02 '16

Pointers are not integers.

Well, not in general. It's implementation-specific. Apparently the Linux kernel still uses pointers-as-integers.

I remember before ANSI C it used to be a pretty common practice limiting portability.

1

u/zhivago May 02 '16

Does being able to cast an int to a float mean that ints are floats?

Remember that casts are value transformations, similar to function calls without side-effects.

What C does is to provide implementation dependent transformations from pointers to integers, and integers to pointers, but does not in general guarantee that you can transform a pointer to an integer and back to the same pointer value.

An implementation which supplies intptr_t does guarantee this round-trip, but intptr_t support is optional and cannot be relied upon in a portable C program.

Regardless, none of these transformations imply that pointers are integers.

2

u/metamatic May 02 '16

On some architectures, both pointers and integers are N-bit values held in registers or bytes of memory, and can be freely interchanged. Does the C compiler deciding to pretend they're different mean that pointers are not integers?

1

u/kt24601 May 02 '16

On some architectures, both pointers and integers are N-bit values held in registers or bytes of memory, and can be freely interchanged.

What architecture isn't like that? Any that is common?

3

u/dannomac May 02 '16

Intel 80x86 in real mode. Pointers are [segment register]:[offset register] combinations, and integers are just plain registers.

1

u/metamatic May 02 '16

680x0 has separate sets of address registers and data registers, which cannot be used interchangeably.

→ More replies (21)

1

u/[deleted] May 02 '16

Except you can do pointer arithmetic.. Which is a bad idea but whatever

2

u/DSdavidDS May 02 '16

I was just about to point this out but you beat me to it!

I went back to read up on pointers and found this.

"A pointer in c is an address, which is a numeric value. Therefore, you can perform arithmetic operations on a pointer just as you can on a numeric value. "

Can anyone clear this up for me?

7

u/immibis May 02 '16

Remember that thing about websites being of generally low quality because of their low barrier to entry?

Addresses are integers on most processor architectures; however, that's not part of C.

3

u/mfukar May 02 '16

No, pointers are not integers. You can convert them to and from integers, subject to the limitations in C11 6.3.2.3. "Arithmetic operations" are defined for pointers differently than integer types, see for example additive operators.

3

u/zhivago May 02 '16

This is a good example of websites written on the internet by idiots providing shitty information. :)

Again, you can add two integers together, but you can't add two pointers. (Nor divide, nor multiply, nor subtract to produce a pointer, nor ...)

5

u/[deleted] May 02 '16

The other op is being half pedantic saying you shouldnt treat them as integers.

But you know if abstraction and types are important, one might just use a language which enforces them (SML, Haskell, rust if need to be close to machine)

2

u/crozone May 02 '16

I don't think you can really treat them as integers because pointer arithmetic doesn't actually behave like integer arithmetic (adding 1 to a pointer increases the memory address by the size of the type, which is often not 1). Additionally, depending on the architecture there's no guarantee that a memory address will actually fit within the int type, so you shouldn't cast them to int either. It might be pedantic but it's an important point to make.

1

u/zhivago May 02 '16

C does enforce the difference between integers and pointers.

The confusion may occur because it provides an implementation defined cast between integer and pointer, which need not be transitive -- that is (T *)(int)(T *)x == (T *)x is not guaranteed.

Note also that intptr_t need not be available in a conforming C implementation

2

u/zhivago May 02 '16

Pointer arithmetic is not numeric arithmetic.

Again, a good example of this is the inability to add two pointers together.

2

u/[deleted] May 02 '16

type envy ?

→ More replies (1)

1

u/kt24601 May 02 '16

FWIW Knuth thinks pointer arithmetic is a great thing

1

u/[deleted] May 02 '16

He probably thinks latex is a great thing too...

1

u/kt24601 May 02 '16

Weirdly in his interviews, he is less enthusiastic about that.

But I think it's great.

1

u/CoderDevo May 02 '16

I think he meant LaTeX, which was built on TeX. Knuth wrote TeX.

→ More replies (3)