LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-09-2012, 03:24 AM   #1
utkarshrawat
Member
 
Registered: Jul 2007
Posts: 120

Rep: Reputation: 15
constant pointer to variable char


If I am declaring pointer like this

int *const cpi;

& I want to assign some value to this pointer ,how should I do it .I did like this but error exist

Code:
main()
{
        int const ci = 123;
        int *const cpi;
  	
        cpi = &ci;
	printf("%d",*cpi);
 
}
OUTPUT IS
Code:
const.c: In function ‘main’:
const.c:9: error: assignment of read-only variable ‘cpi’
 
Old 03-09-2012, 05:56 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Try to get into the habit of initializing your variables when you declare them.

Code:
#include <stdio.h>

int main()
{
    int const ci = 123;
    const int *const cpi = &ci;

    printf("%d\n",*cpi);

    return 0;
}
Note, these two style of declarations are the same:
Code:
int const ci = 123;

/* and */

const int ci = 123;
Personally, I prefer the second style; it is more intuitive for me to comprehend. And because you opted to declare ci as a const int, it was necessary to declare cpi such that the value it points to also would be treated as const.

Alternative declarations for cpi could be:
Code:
int const *const cpi = &ci;

/* or even */

const int const* cpi = &ci;
Again, I prefer the last style, although I rarely ever declare the pointer itself as const. All I typically care about is that the value that is being referenced is const.

Last edited by dwhitney67; 03-09-2012 at 06:00 AM.
 
Old 03-09-2012, 10:21 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Also consider that, even if you say that something is "constant," if you start developing addresses to that data, the compiler really can't keep you from shooting yourself in the foot anymore. Be very careful that you know what you are telling the compiler, and that what you are telling the compiler is really what you want to say. Study the compiler's documentation and the language references carefully.

"Well, it compiles..." is not enough.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Invalid conversion from constant char to char coltson Programming 8 11-07-2011 07:34 AM
char variable is behaving as unsigned char manohar Programming 3 02-15-2011 12:19 AM
How do I point a char pointer to a part of another char array? trist007 Programming 8 11-06-2010 07:56 PM
deprecated conversion from string constant to ‘char*’ knobby67 Programming 3 07-03-2010 02:29 PM
[SOLVED] Why is char* a string and not a pointer to a char? el_b Programming 2 09-25-2009 10:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration