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
Related
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 26 days ago.
Improve this question
I am using char arr[] insidea struct
I used temporary array to get string from user and it works fine ,
when I try to copy this temporary array to the variable array inside the struct I get \command files at end of string
for (u32 i=0;i<name_size;i++)
{
printf("\nin While");
pn->name[i]=temp_name[i];
}
if you must do it byte by byte then do this
u32 i;
for (i=0;i<name_size;i++)
{
printf("\nin While");
pn->name[i]=temp_name[i];
}
pn->name[i] = '\0';
ie - add the trailing zero. simpler would be strcpy
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.
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 needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
When I compile my C codes sometimes I get this error message.
Mycode.exe has stopped working..
A problem caused the program to stop working correctly.
Windows will close the program and notify you if a solution available.
My C code :
#include<stdio.h>
main(){
char a;
a="S";
printf("%s",a);
}
So what is the reason for this problem?
Syntax error, Runtime error or another reason?
When you invoke printf with %s it means that printf will start printing at the given address and end when a null terminator is reached, because you are giving printf a char and not a pointer to a char, it tries to use the value written in a to start printing from.
a is a char taking up a space of one Byte while an address is 8 Bytes in a 64 bit system, so basically printf takes the value in 'a' and the the next 7 Bytes(which are random 'garbage') and tries to use it as an address to stop printing.
that is why it sometimes works as you said, sometimes those random addresses are fine to start printing from, but sometimes they are addresses that you are not authorized to access like areas of memory used by the OS or kernel.
to solve the problem you need to make a a char * and not a char, and assign it with a string.
Change your code to
#include<stdio.h>
int main()
{
char a;
a='S';
printf("%c",a);
return 0;
}
and it will work fine.
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 6 years ago.
Improve this question
I need to memorize how much memory I allocated with realloc().
Help me
if(!array)
array=(Type*) calloc(1,sizeof(Type));
else
array=(Type*)realloc(array,(cont+1)*sizeof(Type));
array[cont].setName(....);
cont++;
It doesn't work: after firt insert, it say: Access violation
I initialized the cont = 0 in the constructor of my class and freed memory in the destructor.
See the comments added to your code:
int count=0;
if(!array)
array=(Type*) calloc(count,sizeof(Type*); // Problem:
// missing )
// use sizeof(Type)
// calling calloc with count being zero
// so you do not allocate any memory
// use 1 instead of count
array[c].setName(EditName->Text);
c++;
count++;
array=(Type*)realloc(array,count*sizeof(Type*)); // Problem:
// use sizeof(Type)
so it should look:
int count=0;
if(!array)
array=(Type*) calloc(1,sizeof(Type));
array[c].setName(EditName->Text);
c++;
count++;
array=(Type*)realloc(array,count*sizeof(Type));
The variable c must be initialized to zero before running this code
Likewise array must be nullptr before running this code
EDIT
There seem to be one more problem if you intend to run this code several times (which I assume you do).
This line:
array=(Type*)realloc(array,count*sizeof(Type));
^^^^^
Don't use count here as you always sets count to zero
The line shall be:
array=(Type*)realloc(array,c*sizeof(Type));
In general there seems to be no real use of count