Member Avatar for I_m_rude

hi...

typdef char * hello;
const hello ptr1,ptr2;

So, in this snippet, my question is that what is constant ? is ptr1 constant or the value to which ptr1 will "point to" will be constant ? and will u please give reason also?

my thinking: const hello means (const char * ptr1). means ptr1's value will be constant not ptr1 itself. in case, it is char * const ptr1, then ptr1 will be constant. but answer of above question I asked is that "ptr1 is constant". please make me correct in my approach.

thanks in advance. any help will be thanful.

Recommended Answers

All 25 Replies

This:

const hello ptr1, ptr2;

Is equivalent to this:

const char *ptr1, *ptr2;

And this:

const char *ptr1;
const char *ptr2;

And as we already know, it's the pointed to value that's const in these cases, not the pointer. To make the pointer const, and thus unable to point to any address other than the one originally assigned, but not the pointed to value, you'd do this:

char * const p = /* An initializing address */;

To make both const:

const char * const p = /* An initializing address */;

Confused yet? Do you hate C's declaration syntax yet? If not, don't worry...you will as soon as you start working with pointers to functions and pointers to arrays. ;)

Member Avatar for I_m_rude

Ya! i already know everything which you have told me. but you haven't answered my question. I think the answer in book is wrong. because in book, answer is that "ptr1 is constant!". is this wrong ? I have mentioned the same question from the book in first post. please check!

Nevermind, I'm wrong. The const applies to the whole of the typedef, so this:

const hello ptr1, ptr2;

Is equivalent to this:

char * const ptr1;
char * const ptr2;

Sorry for the misleading information, I'll blame it on rarely using constant pointers and never wrapping pointers in a typedef. ;)

Member Avatar for I_m_rude

please explain the reason also. I am not getting it.

please explain the reason also.

Um...because that's the way the designers wanted it? It makes sense for a qualifier on a typedef'd type to apply to the object of that type rather than some random part of that type. This is consistent with the behavior of const in the absence of typedef, and consistency is a good thing.

Member Avatar for I_m_rude

means typedef was of char* type in this case which make ptr1, a pointer to char. right ? and applying const to that make const the "object" which is ptr1 which in turn was pointer to char also. so ultimately, ptr1 is constant pointer to char. am i right ? (I know my explainations are damn tuff to understand :P)

Yes, that seems correct.

Member Avatar for I_m_rude

hey. can you please tell that when i write

typdef struct node *hello;

struct node
{
     int a,b;
     hello a1,a2;
};

when compiler is reading typdef line, then struct node is not known to compiler, but why this statement is valid ? can u tell this thing ?

You can create a pointer to an incomplete type. In fact, that's precisely what happens with the node pointer member here:

struct node {
    int data;
    struct node *next; /* struct node is an incomplete type here */
};
Member Avatar for I_m_rude

is this case with pointer only or with some other types also ?

What do you mean "with some other types"?

Member Avatar for I_m_rude

I mean is this case happens with pointers only or with other things also? i am not getting word to ask or to tell what this mean. i mean as this is the case with pointer . it is ok. is this case happen with other things also in C like any other data type or something. I hope i am making my clear.

Not clear at all, try giving me an example.

Member Avatar for I_m_rude

@decptikon i can write

typedef struct node *ull;

struct node
{
      int a;
      ull p;

}hello;

i can write that typedef line before writing the structure as u said that we can make pointer to an incomplete type.

so, can i write

typedef struct node ull;

before the structure is defined ?

secondly, what is the difference between declaring a structure and defining a structure.
thanks. waiting for the reply.

Yes, you can do that. But the use of ull in the structure definition needs to specify a pointer:

typedef struct node ull;
struct node
{
      int a;
      ull *p;
}hello;

secondly, what is the difference between declaring a structure and defining a structure.

From the C standard:

A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that:

  • for an object, causes storage to be reserved for that object;
  • for a function, includes the function body;
  • for an enumeration constant or typedef name, is the (only) declaration of the identifier.

So according to the C standard, this is a declaration:

struct node;

But so is this:

struct node {
    int data;
    struct node *next;
};

However, this is a definition of head and a declaration of the struct in one statement:

struct node {
    int data;
    struct node *next;
} head;

I prefer the C++ terminology that has struct node; as a declaration but listing out the members specifies a definition of the structure. That's simpler, in my opinion, and more consistent, than having both as declarations.

commented: nice one! +0
Member Avatar for I_m_rude

Can you please give me examples of more declarations ? what do you mean by "attributes" and "interpretations" of identifiers ? So that I can clearly get declarations and defintion difference.

This is a declaration:

int add(int a, int b);

It specifies that add is a function returning an integer that takes two integer parameters. This is the function signature which tells the compiler the "attributes" and "interpretation" of the symbol add.

This is a definition:

int add(int a, int b)
{
    return a + b;
}

Because a body is provided, the declaration becomes a definition, but it still also functions as a declaration.

Here's a summary of object declarations and definitions at either block or file scope:

int a;        /* This is a (tentative) definition */
int b = 0;    /* This is a definition */
extern int c; /* This is a declaration */

int main(void)
{
    int d;        /* This is a definition */
    int e = 0;    /* This is a definition */
    extern int f; /* This is a declaration */

    return 0;
}

Given your eye for detail I'll recommend that you don't concern yourself with tentative definitions for now (because I'm sure you'd ask about it otherwise), just follow the more sensible concept from C++ that it's a definition unless you specifically make it a declaration with the extern keyword.

Member Avatar for I_m_rude

hey. EXcellent. I got it now.
hey @deceptikon Can you please tell what is difference between "null pointer", "NULL macro", null string, null character and null pointer constant ?
thanks in advance.

Can you please tell what is difference between "null pointer", "NULL macro", null string, null character and null pointer constant ?

I'm starting to wonder if you're actually a beginner to C or just seeding the forum with questions. ;)

Anyway, a null pointer is the concept of a pointer that points to nothing in particular, but can be predictably tested. That's as opposed to an uninitialized pointer, which could potentially point to anything and thus can't reliably be checked for validity.

A null pointer constant is the concrete form of an initialization value for a null pointer. It's essentially any integer value that equates to 0, either cast or implicitly converted to pointer context.

The NULL macro is a symbolic constant representing a null pointer constant. In C you'll typically see one of these two definitions:

#define NULL ((void*)0)

or

#define NULL 0

In C++ the former is problematic in light of more stringent type system, so NULL is defined simply as 0. But because C allows an implicit conversion from void* to any other pointer type (except a function pointer), NULL can safely include a cast to void*. However, because many C compilers are also C++ compilers, defining NULL as 0 instead of ((void*)0) appears to more common.

Moving away from pointer context, the null character (sometimes referred to as NUL, though there's no standard macro for it) is a character with the value 0. The escape character is '\0', though you could technically use an integer 0 to the same effect:

char a[10];

a[0] = 0; /* Assign a null character */

All valid strings in C end with a null character, as you've probably already learned. A null string isn't standard or conventional terminology, but I'd wager that anytime you hear about it, it means an empty string. An empty string is where the first character is the null character. You can use empty quotes to create one without directly assigning a null character, because string literals always include a null character:

char a[10] = ""; /* Create an empty string */

One thing to note is that even though 0, NULL, and '\0' are generally the same thing (barring when NULL includes a cast, of course), it's poor practice to mix and match them. 0 should only be used in numeric context, NULL only in pointer context, and '\0' only in character/string context. This is for readability purposes, because using the appropriate form of zero will make your intentions more clear.

Member Avatar for I_m_rude

Do you really think i am seeding forum with questions ? don't you think what so ever question i have asked is a genuine question and should be asked by a beginner ? I am astonished when you said that line.

Do you really think i am seeding forum with questions ?

I have my suspicions from many years answering beginner questions, but it's totally irrelevant. I'd answer your questions regardless, if only to make sure that there's at least one clueful response. Nothing is more damaging to beginners' progress than being mislead by another beginner. And while we don't discourage beginners from trying to help, it's beneficial to have experienced folks around to offer corrections when needed. ;)

I am astonished when you said that line.

It's not an insult. You strike me as unusually observant and thorough. You catch nuances that aren't commonly caught, and you ask questions in a way that have a hint of greater understanding.

Member Avatar for I_m_rude

ont thing, Doesn't compiler give the error if array limits are crossed ? array may be character , integer or whatever. On google.com, somewhere it is written that it will give, and some where that it will not give. what is exact answer ?

please tell.

Doesn't compiler give the error if array limits are crossed ?

It's undefined behavior, anything could happen.

Member Avatar for I_m_rude

sir, Is it your work experience that make you to tell each and every thing in fraction of seconds or how you tell all these things in a moment ?

For the most part. For everything else there's google.

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.