| | |
Array of string
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2008
Posts: 5
Reputation:
Solved Threads: 0
How can I change a character of a string within an array of strings?
An execution error happens if I run the code below..
Grato
An execution error happens if I run the code below..
c Syntax (Toggle Plain Text)
int main(int argc, char *argv[]) { char *d[] = {"aaaa","bbbb","cccc"}; cout << d[2] << endl; //PRINT cccc d[2][2] = 'x'; cout << d[2] << endl; // IT SHOULD BE PRINTED ccxc BUT AN EXECUTION ERROS HAPPENS }
Last edited by Narue; Oct 16th, 2008 at 2:35 pm. Reason: added code tags and formatting
char *d[] = {"aaaa","bbbb","cccc"}; These strings reside in read only memory. They can not be altered. You can not re-assign to them, without raising an error.d[2][2] = 'x';>How can I change a character of a string within an array of strings?
char *d[] is an array of pointers to string.What you want is
char d[][5] Your code is not C but C++, there's a different forum for C++, since they are not the same language.
Last edited by Aia; Oct 15th, 2008 at 8:40 pm.
•
•
Join Date: Oct 2008
Posts: 5
Reputation:
Solved Threads: 0
Thanks for your reply Aia,
sorry I know that this code is C++, but my problem is also in C !!
char *d[] = {"aaaa","bbbb","cccc"};
you said that these strings cannot be altered
but if we do something like:
d[2]="xxxx";
it will not raise an error
there is no way to alter a character in a array of pointers to string ?
sorry I know that this code is C++, but my problem is also in C !!
char *d[] = {"aaaa","bbbb","cccc"};
you said that these strings cannot be altered
but if we do something like:
d[2]="xxxx";
it will not raise an error
there is no way to alter a character in a array of pointers to string ?
You wouldn't want to do that, I think it's undefined behaviour when you try to do that. Why can't you create your array of char arrays like Aia said?
c Syntax (Toggle Plain Text)
char d[][5]
•
•
Join Date: Feb 2008
Posts: 12
Reputation:
Solved Threads: 1
As I mentioned, this code worked fine for me as written once I included the iostream library. However, as per Aia's comment, this might be because I'm using the Watcom C++ compiler - perhaps there are inane subtleties because of compiler flavor?
Anyway, I noticed this thread - http://www.velocityreviews.com/forum...f-strings.html - which addresses the problem in C++ using vectors which might be a better way to do it though it's impossible to tell without knowing more about the larger problem.
Anyway, I noticed this thread - http://www.velocityreviews.com/forum...f-strings.html - which addresses the problem in C++ using vectors which might be a better way to do it though it's impossible to tell without knowing more about the larger problem.
•
•
•
•
Thanks for your reply Aia,
sorry I know that this code is C++, but my problem is also in C !!
char *d[] = {"aaaa","bbbb","cccc"};
you said that these strings cannot be altered
but if we do something like:
d[2]="xxxx";
it will not raise an error
there is no way to alter a character in a array of pointers to string ?
char *d[] = {"aaaa","bbbb","cccc"}; is an array of pointer. Individually represented would be as:d[0] = "aaaa";
d[1] = "bbbb";
d[2] = "cccc";
They are pointing to some piece of memory that you can't change due to how you declared it initially.
Any of those subscripts holds the address of where that piece of memory is. Which memory you can not re-assign to it.
d[2] = "xxxx"; is just changing where the pointer (variable d[2]) is pointing to. Not the content. That's possible thanks to the way you declared it and initialized it. Now it is pointing to another place, by virtue of holding the address of the beginning of that memory, which contains "xxxx"However you just left behind a piece of memory containing 'c''c''c''c''\0' and there's no way to access to it anymore. Commonly named "memory leak".
Last edited by Aia; Oct 16th, 2008 at 7:21 pm.
![]() |
Similar Threads
- Java's String Tokenizer (Java)
- Converting byte array into string (ASP.NET)
- Help picking numbers out of an array/string (C++)
- Conversion of Long Array to String (Java)
- Array/String intersect and Array/Array intersect code (C#)
- Conver int Array into a String (Java)
- Array to String (PHP)
- sorting an array of string (C)
Other Threads in the C Forum
- Previous Thread: UTF8, Multibyte, Wide Character, Ascii
- Next Thread: database - turbo c
| Thread Tools | Search this Thread |
* ansi api append array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlogicaldrivestrin givemetehcodez grade graphics gtkwinlinux histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft multi mysql oddnumber open opendocumentformat openwebfoundation overwrite pdf pointer pointers posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation threads unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






