Thread: constant pointer?

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    8

    constant pointer?

    I have been given a header file to work from as an assignment, and a constant pointer is used: int * const acctFd

    Can I alter the value of this pointer? If so, how? Its a file descriptor, and I have been trying to alter it using: fopen('pathname', "r+"); and then return it, however I cannot seem to set it.

    Any ideas?

    Thanks,

    Chris

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    This code looks like C++. If that is the case, then you cannot modify the value of the pointer without casting, but you can modify what the pointer points to.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Code:
    fopen('pathname', "r+");
    needs to be

    Code:
    fopen("pathname", "r+");
    Not sure if you did that just for clarity or if your actual code is written that way (usually when you're putting something like that, you do <pathname> to indicate a value goes there).

    I'm just pointing this out because passing a string with single quotes (which are used for character literals) will cause a compiler error.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Any ideas?

    Assignment is different than initialization.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       int i = 42;
       int *const p;
       int *const q = &i; /* initialization */
       /*p = &i; // assignment - error */
       printf("*q = %d\n", *q);
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-15-2009, 08:38 AM
  2. Constant pointer to constant value
    By tretton in forum C Programming
    Replies: 10
    Last Post: 12-23-2005, 01:45 PM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM