Questions tagged [character-arrays]
135 questions
167
votes
9 answers
PHP - iterate on string characters
Is there a nice way to iterate on the characters of a string? I'd like to be able to do foreach, array_map, array_walk, array_filter etc. on the characters of a string.
Type casting/juggling didnt get me anywhere (put the whole string as one…

jon_darkstar
- 16,398
- 7
- 29
- 37
34
votes
7 answers
When should std::string be used over character arrays?
I sit around and think about whether or not I should be using const char* or const std::string& pretty often when I'm designing class interfaces and when it comes down to it, I often feel like there's 6 in one hand with half a dozen in the…

vmrob
- 2,966
- 29
- 40
22
votes
1 answer
"Semantic issue: Implicitly declaring library function 'malloc' with type 'void *(unsigned long)'"
I have a block of code where I'm trying to grab an expression inside of parentheses and then use it. At the point where the below code begins, I am in the middle of iterating through a character array and pcc is the pointer to the current character,…

Lily Carter
- 273
- 1
- 2
- 5
15
votes
5 answers
Making an Array to Hold Arrays of Character Arrays in C
My C is a little more than rusty at the moment, so I'm failing to create something I think should be pretty basic.
Allow me to refer to character arrays as strings for this post. It will make things clearer for both me and you.
What I have is an…

Dae314
- 385
- 1
- 2
- 9
9
votes
4 answers
Comparing character arrays and string literals in C++
I have a character array and I'm trying to figure out if it matches a string literal, for example:
char value[] = "yes";
if(value == "yes") {
// code block
} else {
// code block
}
This resulted in the following error: comparison with string…

Ian Burris
- 6,325
- 21
- 59
- 80
7
votes
3 answers
Removing punctuation and capitalizing in C
I'm writing a program for school that asks to read text from a file, capitalizes everything, and removes the punctuation and spaces. The file "Congress.txt" contains
(Congress shall make no law respecting an establishment of religion, or…

Zach Greene
- 73
- 3
6
votes
4 answers
How can I remove 'garbage input' from a C string?
I am attempting to write a function that will remove all characters from an array, except for '+', '-', '*', '/', and numbers. This is the code I came up with:
void eliminateJunk(char string[MAX]){
int i,j;
char stringOut[MAX];
int…

jkm
- 103
- 1
- 1
- 5
6
votes
4 answers
Clarification of char pointers in C
I'm working through K&R second edition, chapter 5.
On page 87, pointers to character arrays are introduced as:
char *pmessage;
pmessage = "Now is the time";
How does one know that pmessage is a pointer to a character array, and not a pointer to a…

retrodev
- 2,323
- 6
- 24
- 48
6
votes
2 answers
Why adding a Null Character in a String array?
I know that we have to use a null character to terminate a string array like this:
char str[5] = { 'A','N','S','\0' };
But I just wanted to know why is it essential to use a null character to terminate an array like this?
Also why don't we add a…

Anurag-Sharma
- 4,278
- 5
- 27
- 42
6
votes
1 answer
Copying the content of a character array to a QString in Qt
I have a character pointer that in any run can have different length. For example:
char* myChar;
In one run its content can be "Hi" and in another run it can be "Bye".
I want to copy the content of myChar to a QString, for example if I…

TJ1
- 7,578
- 19
- 76
- 119
6
votes
4 answers
C++: Does strcat() overwrite or move the null?
Now lets see this small program
char s[20]="One";
strcat(s,"Two");
cout<

Mohamed Ahmed Nabil
- 3,949
- 8
- 36
- 49
5
votes
4 answers
Why is a char* being treated the same as a char** in C?
I have the following test application:
#include
#include
#include
int main(void){
char buf[512];
buf[0]= 0x1;
buf[1]= 0x2;
char *temp1 = &buf;
char *temp2 = buf;
char *temp3 = &buf[0];
…

austinmarton
- 2,278
- 3
- 20
- 23
5
votes
2 answers
How to repeat a character multiple times as a MATLAB array?
Given a single string value in a MATLAB character array:
['12 N']
How can I repeat this value X times in a new character array?
For example:
X = 5
['12 N'; '12 N'; '12 N'; '12 N'; '12 N']

Borealis
- 8,044
- 17
- 64
- 112
5
votes
2 answers
Doesn't %[] or %[^] specifier in scanf(),sscanf() or fscanf() store the input in null-terminated character array?
Here's what the Beez C guide (LINK) tells about the %[] format specifier:
It allows you to specify a set of characters to be stored away (likely in an array of chars). Conversion stops when a character that is not in the set is matched.
I would…

Rüppell's Vulture
- 3,583
- 7
- 35
- 49
5
votes
5 answers
Program skips cin.getline()
I have made this program, It get the users adress, name and work. Then It puts it all into one string and outputs that string. (I know there are better ways to do this)
char str[600];
char adrs[200];
char name[10];
char wrk[200];
cout<<"Enter your…

Mohamed Ahmed Nabil
- 3,949
- 8
- 36
- 49