Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I need to fill manually an array of chars. I declared it like this:
char* coor = malloc(sizeof(char) * 5);
Then I manually assigned every variable to its position:
coor[0] = O_colum;
coor[2] = ((char)(O_row+48));
coor[3] = '-';
coor[4] = D_colum;
coor[5] = ((char)(D_row+48));
(D_Row and O_row are integers, I need that number in character form, not the equivalent value in ASCII; that’s why I do +48)
The problem comes when I try to print it. If I use printf(" %s", coor) it only prints the first characters and I don’t know why. I’m using %s, so it should print all the characters in the string.
When I do this:
char *p = "hello";
printf("%s",p);
It does print hello.
There are two mistakes in your code:
you are skipping the position 1 of the array. This is probably the
reason why it prints only the first element.
you need to add the end string character \0 in the end of the
string.
This should fix it :
char* coor = malloc(sizeof(char) * 6);
coor[0] = O_colum;
coor[1] = ((char)(O_row+48));
coor[2] = '-';
coor[3] = D_colum;
coor[4] = ((char)(D_row+48));
copr[5] = '\0';
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
In C if I wanted to iterate through the array and get the ascii value of each letter would I do it by
const char array[] = "name"
for (int i=0; i<4; i++){
printf("%s", array['i']);}
I am still unfamiliar with C and would appreciate any pointers.
The format specifier %s means "print a string" so that's not what you want. If you want the ascii value in decimal use %d or %u. If you want the ascii value in hex use %x.
Also notice that the indexing is array[i]
Further, use strlen instead of a hard coded 4. By doing that you can change "name" to "WhatEver" without having to change the loop condition as well.
Like:
const char array[] = "name";
for (size_t len = strlen(name), i = 0; i < len; i++)
{
printf("%d ", array[i]);
}
puts("");
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
char hi[10] = "bye";
char a = 'a';
strcat(hi, a);
Like the example above. How would I do this in C? Is there a more general string I cant let hi be?
a is a char type while strcat expects it's both arguments of type char *. To append the char to an array of characters you can do this
int index = strlen(hi);
if(index < sizeof(hi)-1)
hi[index] = a;
Note that in this particular case the initializer will initialize the first three elements of hi to b, y and e respectively. The rest of the elements will be initialized to 0. Therefore you do not need to take care of the null termination of the array after appending each character to it. But in general you have to take care of that.
int index = strlen(hi);
if(index < sizeof(hi)-1){
hi[index] = a;
hi[index+1] = '\0';
}
strcat(hi, (char[]){a,0});
This would append the a.
Or you can do this
char s[]={a,0};
strcat(hi,s);
Or simply
#define MAXLEN 10
...
size_t len = strlen(hi);
if( len+1 <= MAXLEN-1)
hi[len]=a,hi[len+1]=0;
else
// throw error.
In your case hi[len+1]=0 is not required as it is already filled with \0. Also as mentioned by Serge that you can use simply used the string literal as the second parameter to the strcat function.
strcat(hi,"a");
There is a subtle difference in this two as mentioned by Serge again, that string literals are const but the compound literals are not.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm doing the CS50 course, pset2 initials. The only error I get is a problem with get_string: it would have an 'incompatible pointer types initializing 'string' with an expression of type 'string (void)'. I really don't understand what I am doing wrong, because my code for get_string worked for the last problem set.. Here's my code:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
// ask user for input
printf("What are your names?");
string s = get_string;
// print first letter from string & capitalize
printf("%c", toupper(s[0]));
//iterate over characters in current string + start loop
for (int i = 0; i < strlen(s); i++)
{
//find space character
if (s[i] == ' ')
{
// print character next to space & capitalize
printf("%c", toupper(s[i++]));
}
// new rule
printf("\n");
}
}
Although you have not shown us cs50.h, we can guess that get_string is a function, so this:
string s = get_string;
Needs to be:
string s = get_string();
get_string is a function that takes no parameters:
string get_string(void);
so
string s = get_string;
must be
string s = get_string();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to count a char*, but my looping never stop until it reach the last part of memory allocation...?
Here's what I mean:
char* text1 = "Hello Guys!";
char* text2 = "i dont know why";
char* text3 = "Hello World";
int counter = 0;
for(char* temp = text1;temp != '\0';)
{
++temp; ++counter;
}
//then i printed Counter
But for some reason, my counter shows 106506.
When I debug those, after temp shown "!" and then it goes to "" it keeps going. :(
and after a few step, my char* (temp) reach the text2 memory, and text3 memory. "I don't know why" and "Hello World" are counted as well.
temp is a pointer and it won't be equal to '\0', which is 0, in the loop.
To access to the character pointed at by that, use *temp.
First of all you should not assign string literal to char * even if compiler allows that for compatibility, second you do not need additional pointer:
const char* text1 = "Hello Guys!";
int counter = 0;
for( ; text1[counter]; ++counter );
but better use strlen():
int counter = strlen( text1 );
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to make a program to find and replace some text in a string at the moment I'm trying to change "hello how are you" to "hello bow are you" as a test.
So firstly I find the "how" by using char *substring = strstr(mystring, newstr);
which returns a pointer to "(this position)how are you" now I have no idea how to change the next 3 letters. I can strlen(newstr) for the length of the string I'm replacing "how" with but I can't find a way to change mystring starting from the pointer newstr.
Change the first character by subscripting the substring.
substring[0] = 'b';
If you want to replace multiple characters, try a loop, or use memcpy. Don't use strcpy: you don't want the NUL terminator to be copied.
memcpy(substring, "how", 3);
*substring = 'b'; as posted by user EOF my solution was as so
for (int x = 0; x < strlen(newstring); x++){
*substring++ = newstring[x];
}