Sponsored Content
Top Forums Programming concat const char * with char * Post 302288503 by jim mcnamara on Tuesday 17th of February 2009 12:03:39 PM
Old 02-17-2009
I get this with a C99-compliant compiler:
Code:
csaprd:/home/jmcnama> cc t.c
cc: "t.c", line 6: error 1549: Modifiable lvalue required for assignment operator.
cc: "t.c", line 7: warning 611: Type conversion loses "const" qualifier.
cc: "t.c", line 7: warning 563: Argument #1 is not the correct type.

on this code:
Code:
#include <string.h>
#include <stdlib.h>

const char *mystrcpy(const char *dest,const char *source, const size_t len)
{
    *dest=0x0;
	memcpy(dest, source, len);
	return dest;
}

int main()
{
    char dest[10]={0x0};    
    const char *p=mystrcpy(dest, "test", 4);
    printf("%s\n", dest);
    return 0;
}

const char *foo means it cannot be modified (the "modifiable lvalue" complaint), for example. ujeshm may use a non-standard compiler or a really old one that allows it.

You can work around it by copying a const char into a regular string then modifying the new string.

Note: A warning means the code did not compile correctly, and if it runs you are then coding by coincidence and will sooner or later cause serious problems that are difficult to resolve.

If you are using gcc, compile
Code:
gcc -Wall mycode.c

to see full errors and warnings.
 

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
STRCAT(3)						     Linux Programmer's Manual							 STRCAT(3)

NAME
strcat, strncat - concatenate two strings SYNOPSIS
#include <string.h> char *strcat(char *dest, const char *src); char *strncat(char *dest, const char *src, size_t n); DESCRIPTION
The strcat() function appends the src string to the dest string, overwriting the terminating null byte ('') at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result. If dest is not large enough, program behavior is unpredictable; buffer overruns are a favorite avenue for attacking secure programs. The strncat() function is similar, except that * it will use at most n bytes from src; and * src does not need to be null-terminated if it contains n or more bytes. As with strcat(), the resulting string in dest is always null-terminated. If src contains n or more bytes, strncat() writes n+1 bytes to dest (n from src plus the terminating null byte). Therefore, the size of dest must be at least strlen(dest)+n+1. A simple implementation of strncat() might be: char* strncat(char *dest, const char *src, size_t n) { size_t dest_len = strlen(dest); size_t i; for (i = 0 ; i < n && src[i] != '' ; i++) dest[dest_len + i] = src[i]; dest[dest_len + i] = ''; return dest; } RETURN VALUE
The strcat() and strncat() functions return a pointer to the resulting string dest. CONFORMING TO
SVr4, 4.3BSD, C89, C99. NOTES
Some systems (the BSDs, Solaris, and others) provide the following function: size_t strlcat(char *dest, const char *src, size_t size); This function appends the null-terminated string src to the string dest, copying at most size-strlen(dest)-1 from src, and adds a null ter- minator to the result, unless size is less than strlen(dest). This function fixes the buffer overrun problem of strcat(), but the caller must still handle the possibility of data loss if size is too small. The function returns the length of the string strlcat() tried to cre- ate; if the return value is greater than or equal to size, data loss occurred. If data loss matters, the caller must either check the arguments before the call, or test the function return value. strlcat() is not present in glibc and is not standardized by POSIX, but is available on Linux via the libbsd library. SEE ALSO
bcopy(3), memccpy(3), memcpy(3), strcpy(3), string(3), strncpy(3), wcscat(3), wcsncat(3) COLOPHON
This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. GNU
2012-07-19 STRCAT(3)
All times are GMT -4. The time now is 05:20 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy