well i alawys wondered when i make char name[]="bla"; and count by ptr it doesnt work it says cant count wrong argument to increment
but see whenever i like make it a function like void putit(char *Name);
it work i dunno why though why does it work then if its an array ? does it cast it or something ? like take for example this code here

#include <stdio.h>
//put it function work but how so even i use normal array in name it wasnt a chr ptr ??
void Put_it(char *Name) {
     while(*Name)
         putchar(*Name++);
}
int main(void)
{
    char name[]="blabla";
    while(*name)
        putchar(*name++);//doesnt work
    return getchar();
}

if it does cast it why when i cast in putchar in main function it doesnt work ??

Recommended Answers

All 8 Replies

i think i knew why coz Name is a char ptr does it get assigned to the address of the Name then it can increment address coz its char ptr ?

but if it does why the hell you cant cancantate stuff in main when u do char *name="bla"; but in a function u can do it ?? thats so weird thanks guys in advance

char name[] in essence is a label that references the memory at that location. It is not a pointer.

char *p = name;
p is a pointer, occupies memory as a pointer that contains the address of memory that it references. Because it is a pointer, pointer math can be utilized that changes the value in memory that is the pointer.

For example the following are only sample memory locations, not absoute!

char name[] = "ABC";
Memory: 0x0012ff44 0x41 0x42 0x43 0x00 "ABC",0
&name (address of name) is 0x0012ff44

char *p = name;
&p is 0x0012ff38
Memory: 0x0012ff38 0x44 0xff 0x12 0x00
The four bytes that is <p> contains the address of the string assigned to it!

And if we did a
p++;
then that address is
Memory: 0x0012ff38 0x45 0xff 0x12 0x00

oh thanks man i understand
p has same address of first element of name[] but the char *name itself is in diff memory right ?

I wouldn't get bogged down with the concept of different memory.

Just think of it in simpler terms.
p is a label to apoint in memory that contains four bytes. Those four bytes are used as a 32-bit pointer that points somewhere else in memory.

name is a label to a point in memory that contains the ASCIIz string.
It has no space. It is only a reference label. Like p is a reference label to the four byte pointer memory.

I'm kind of over simplifying it. but that's as simple as I can make it.

Discussing other kinds of memory (segments) can get very complicated, and for purposes of this discussion, no need to go there! that's a totally different level.

Another way to think of this, look at the index of a book. A book about animals. So in the Index lookup bears. Bears is on page 32.

If you go to page 32 you see the topic Bears.

The index in the back of the book is the label. The page number is the pointer at that label.

commented: That's a good little analogy. +1

oh cool thanks mate nice way of putting it to

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.