How to count a char*? [closed] - c

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 );

Related

Size of char Array doesn't change(C) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 2 years ago.
Improve this question
Somehow my program outputs the same size no matter how long the array gets, do you know what i did wrong?
char charArray[] = "STRING";
int size = sizeof(charArray) / 2 - 1;
printf("%d", size);
Output: 3
(i have to create a program which finds a string in another string thats why i am substracting 1 at the end to find the length of the word i want to find)
If you just want to get the length of your string, you could use strlen from the string library, of implement your own one:
size_t my_strlen(const char *str)
{
size_t i = 0;
while (str[i] != '\0')
i++;
return (i);
}
with this function, my_strlen("STRING") will return 6.

How to append a char var[] to a char ? C [closed]

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.

Why doesn't my strtok() work for () specfically? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
For example it prints '(night' despite tokenizing (), why does this happen?
char* word = strtok(&c, ",.;()");
while(word!= NULL)
{
word = strtok(NULL, ",.;()");
printf("%s ", &c);
}
Your code just prints &c on every iteration (whatever that is). You never print word, which is your next token. That's why you never see the results of your tokenization. If you want to see the tokens, you have to print word, not c.
On top of that it is completely unclear why you are applying & operator to your c. If c is a string pointer or a char array, that & there makes no sense at all.

I can't understand an error in a function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
This function make a strange error after using it several times and I really can't understand the reason behind it.
char *get_range(char *str,int min,int max){
char *_res=(char *)malloc(sizeof(str));
int cur=0;
while (min<max){
_res[cur]=str[min];
min++;
cur++;
}
return _res;
}
The problem is that after using this function several times, the output comes with additional chars and I don't understand why.
Notice: The additional chars are allway used returned by the function beffor
char *_res=(char *)malloc(sizeof(str));
is wrong. sizeof(str) is measuring the size of a char pointer. This is either 4 or 8 (typically) depending on your system (32 or 64 bit).
You need
char *_res=(char *)malloc(strlen(str) + 1);
strlen returns the number of characters in the string, and you need to add 1 for the terminating 0;
Second you have to add a terminating zero at the end, do:
_res[cur] = '\0';
before returning

printing an array of chars ( C) Only first letter [closed]

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';

Resources