Sponsored Content
Top Forums Programming concat const char * with char * Post 302288963 by ujeshm on Wednesday 18th of February 2009 12:01:36 PM
Old 02-18-2009
"const char *foo means it cannot be modified" -> This is not completly correct. 'const char *foo' means foo is a pointer which points to a constant char (pointer to a constant variable, not a constant pointer variable). That means, the address in foo can be changed(foo is not a constant), but we cannot modify the value contained in the address held by foo(the value is considered as constant).

For example

Code:
const char *foo;

foo="Test1";

cout<<"\n foo 1 : "<<foo<<endl;  // Result will be 'Test1'

foo="Foo is changed now."       

cout<<"\n foo 2 : "<<foo<<endl;  // Result will be 'Foo is changed now'

In your code,

you got error in line 6 because, you are trying to change the value contained in the address held by 'dest'. That is if you remove the '*' it wont generate any errors. But the meaning will be different.

you got error in 7 because the dest is a const char * variable. memcpy does not allow const char * as its first argument (memcpy(void *,const void *,size_t))

The work around you are suggesting is similar to what I have given in my previous post. Only difference is you are asking to assign the address of the const char * variable to a char * variable and change the content of the char * variable. I declared the char * variable first and assigned its address to the const char * variable. After that I am also doing the same thing.

I think my previous post created some miss understandings in you. Sorry if it is so.

Last edited by ujeshm; 02-18-2009 at 02:15 PM..
 

10 More Discussions You Might Find Interesting

1. Programming

diff b/w char const in C/C++

hi, what is the difference b/w char in C and C++.and give me the examples. Thanks... sarwan (3 Replies)
Discussion started by: sarwan
3 Replies

2. Programming

char *p and char p[].

Can anyone please explain me the difference between char *p and char p ? Thanks in Advance, Arun. (4 Replies)
Discussion started by: arunviswanath
4 Replies

3. UNIX for Dummies Questions & Answers

int open(const char *pathname, int flags, mode_t mode) doubt...

hello everybody! I want to create a file with permissions for read, write, and execute to everybody using C, so I write this code: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(){ int fileDescriptor; fileDescriptor =... (2 Replies)
Discussion started by: csnmgeek
2 Replies

4. Shell Programming and Scripting

How to replace any char with newline char.

Hi, How to replace any character in a file with a newline character using sed .. Ex: To replace ',' with newline Input: abcd,efgh,ijkl,mnop Output: abcd efgh ijkl mnop Thnx in advance. Regards, Sasidhar (5 Replies)
Discussion started by: mightysam
5 Replies

5. Programming

Adding a single char to a char pointer.

Hello, I'm trying to write a method which will return the extension of a file given the file's name, e.g. test.txt should return txt. I'm using C so am limited to char pointers and arrays. Here is the code as I have it: char* getext(char *file) { char *extension; int i, j;... (5 Replies)
Discussion started by: pallak7
5 Replies

6. Shell Programming and Scripting

how first char in odd line and second char in even line

Hi I m having ifconfig -a o/p like sbanlab1:ksh# ifconfig -a | egrep "flags|inet" | awk -F' ' '{print $1,$2}' lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> inet 127.0.0.1 lo0:1: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> inet 127.0.0.1 bge0:... (1 Reply)
Discussion started by: tarunn.dubeyy
1 Replies

7. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

8. Programming

Using const char*

I am writing some code in C++ to print a message using fprintf Here is an example void pr_desc( FILE* stream, int shift, const char* desc) { const char* format="%*s\e; fprintf(stream,format,shift,"",desc); } I call it using const char* desc; ... (4 Replies)
Discussion started by: kristinu
4 Replies

9. Programming

Double to const char conversion

Dear all, I am using C and ROOT for programming. And I need to incorporate following in my code. char *fps=NULL; int dec=0,sign=0; float mean = h1->GetMean(1); //0.001298 fps= fcvt(mean,6 , &dec, &sign); I need to provide this mean as const char to some other function to get... (8 Replies)
Discussion started by: emily
8 Replies

10. Programming

Invalid conversion from char* to char

Pointers are seeming to get the best of me and I get that error in my program. Here is the code #include <stdio.h> #include <stdlib.h> #include <string.h> #define REPORTHEADING1 " Employee Pay Hours Gross Tax Net\n" #define REPORTHEADING2 " Name ... (1 Reply)
Discussion started by: Plum
1 Replies
STRING(3)						   BSD Library Functions Manual 						 STRING(3)

NAME
stpcpy, strcat, strncat, strchr, strrchr, strcmp, strncmp, strcasecmp, strncasecmp, strcpy, strncpy, strerror, strlen, strpbrk, strsep, strspn, strcspn, strstr, strtok, index, rindex -- string specific functions LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <string.h> char * stpcpy(char *dst, const char *src); char * strcat(char *s, const char * append); char * strncat(char *s, const char *append, size_t count); char * strchr(const char *s, int c); char * strrchr(const char *s, int c); int strcmp(const char *s1, const char *s2); int strncmp(const char *s1, const char *s2, size_t count); int strcasecmp(const char *s1, const char *s2); int strncasecmp(const char *s1, const char *s2, size_t count); char * strcpy(char *dst, const char *src); char * strncpy(char *dst, const char *src, size_t count); char * strerror(int errno); size_t strlen(const char *s); char * strpbrk(const char *s, const char *charset); char * strsep(char **stringp, const char *delim); size_t strspn(const char *s, const char *charset); size_t strcspn(const char *s, const char *charset); char * strstr(const char *big, const char *little); char * strtok(char *s, const char *delim); char * index(const char *s, int c); char * rindex(const char *s, int c); DESCRIPTION
The string functions manipulate strings terminated by a null byte. See the specific manual pages for more information. For manipulating variable length generic objects as byte strings (without the null byte check), see bstring(3). Except as noted in their specific manual pages, the string functions do not test the destination for size limitations. SEE ALSO
bstring(3), index(3), rindex(3), stpcpy(3), strcasecmp(3), strcat(3), strchr(3), strcmp(3), strcpy(3), strcspn(3), strerror(3), strlen(3), strpbrk(3), strrchr(3), strsep(3), strspn(3), strstr(3), strtok(3) STANDARDS
The strcat(), strncat(), strchr(), strrchr(), strcmp(), strncmp(), strcpy(), strncpy(), strerror(), strlen(), strpbrk(), strspn(), strcspn(), strstr(), and strtok() functions conform to ISO/IEC 9899:1990 (``ISO C90''). BSD
December 11, 1993 BSD
All times are GMT -4. The time now is 05:48 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy