CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2012
    Location
    INDIA
    Posts
    25

    Unhappy Const Pointer Plzzzz HELP

    I am unable to get this:

    as we say int const*ptr; means ptr is a pointer to an const int.

    In a example:

    int x = 10;
    int const*ptr = &x;

    //Here *ptr = 12// will give error...

    Then if we modify X = 12;// Why there's no error in this case....

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Const Pointer Plzzzz HELP

    Because x is non-const.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Feb 2012
    Location
    INDIA
    Posts
    25

    Question Re: Const Pointer Plzzzz HELP

    But. if we write:

    void main()
    {
    int x = 10;

    const int*ptr = &x;

    x = 12;

    printf("%d", *ptr);

    }// Here we are modifying value of X , doesn't it mean that we are modifying the value to whom the *ptr is pointing to, i mean if we write *ptr =12 it gives error , then ptr is pointing to x , and if we change x's value then why there's no error.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Const Pointer Plzzzz HELP

    Quote Originally Posted by Tanushreeagr
    Here we are modifying value of X , doesn't it mean that we are modifying the value to whom the *ptr is pointing to
    Yes, but not through the pointer, so that is fine.

    The pointer is a pointer to const int. It turns out that the int itself is not const, but from the point of view of someone using the pointer, the int is const.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Feb 2012
    Location
    INDIA
    Posts
    25

    Question Re: Const Pointer Plzzzz HELP

    Thx , now i got it.... I am unable to What is the use of Function Pointers in C and C++language?.
    TANUSHREE-AGRAWAL...

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Const Pointer Plzzzz HELP

    Quote Originally Posted by Tanushreeagr
    I am unable to What is the use of Function Pointers in C and C++language?
    Read The Function Pointer Tutorials.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Feb 2012
    Location
    INDIA
    Posts
    25

    Unhappy Re: Malloc() function Plzzz Help

    int main()
    {
    int* ptr;
    int n, i;

    printf("How many numbers you want to enter: -");

    scanf("%d", &n);

    if((ptr = (int*)malloc(n * sizeof(int))) == 0)/// if we skip this statement??
    {
    return 0;
    }


    for(i = 0; i < n; i++)
    {
    printf("Enter number:- ");
    scanf("%d", ptr + i);
    }
    printf("\n you have entered:- ");
    for(i = 0; i < n; i++)
    {
    printf("%d ", ptr[i] );
    }
    free(ptr);


    printf("%d", sizeof(ptr));
    printf("\n%d", sizeof(ptr + i));
    return 0;

    }

    In the above program as we know that normally the compiler doesn't enter if statement , then if we skip the if statement why there's is runtime error.
    Last edited by Tanushreeagr; March 20th, 2012 at 06:39 AM. Reason: spealling mistake
    TANUSHREE-AGRAWAL...

  8. #8
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: Const Pointer Plzzzz HELP

    You better open a new thread for a new question!

    Anyway, if you skip the if statement, you also skip the memory allocation for ptr. Thus ptr does not point to a valid address.

  9. #9
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Const Pointer Plzzzz HELP

    The conceptual difficulty here probably is that this if statement does the actual work that needs to be done (the allocation of the memory block) during the evaluation of its condtion. The actual body of the if instruction is only executed when that fails and is of secondary importance.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured