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

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.

Related

Why do I get "\common Files" at the end of string inside my struct [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 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

why doesn't this program in C language print anything? [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
why doesn't this program print anything?
int main(){
int a = getchar()-'0';
getchar();
int b = getchar()-'0';
int vsota = 0;
vs = (a+b)%10;
putchar(vs);
printf("\n");
}
I put in the numbers 7 and 9 and it was supossed to outprint 6 but it does not.
putchar(vs); writes the character whose code is the value in vs. The value in vs is 6. So it writes the character with code 6. That character is not the digit “6”. You do not see anything because it is a “control character” with no visible appearance. To write the character for the digit whose value is in vs, use putchar('0' + vs);.
Also, fix this:
int vsota = 0;
vs = (a+b)%10;
That would not have compiled, so presumably you made a mistake when entering code into Stack Overflow. Use the same name in both places.
because you try to output non printable character
int main(){
int a = '1'-'0';
int b = '6'-'0';
int vsota = 0;
vsota = (a+b)%10;
putchar(vsota + '0');
printf("\n");
}
https://godbolt.org/z/NAyrNn

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

Dynamic Array of strings [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 6 years ago.
Improve this question
I am trying to create an array of strings but I keep getting an error.
Can you help me figure out what's wrong with this code?
int size;
scanf("%d",&size);
char** arr;
arr=(char**)malloc(sizeof(char*)*size);
You can simply use array of n number of pointers to char. Then use a loop to allocate space for those.
int n, size;
scanf("%d %d", &n, &size);
char *arr[n];
for( int i = 0; i < n; ++i ){
arr[i] = malloc( size * sizeof(char) );
}

Printing one dimensional array in C [closed]

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 7 years ago.
Improve this question
In Python I can define a list, and print it all with one command:
lst = [1,2,3]
print lst
[1,2,3]
Is there any equivalent command in C ? (without using loops), or do I have to print every element by himself?
Thanks!
You can use recursion like in this pseudo code:
void print_array(item array[], size_t len)
{
if (len == 0)
return;
print_item(*array);
print_array(array + 1, len - 1);
}
Modern C compilers can optimize away tail-recursion, so this is likely not much less efficient than a loop.
If it's a string then yes, you can dump the entire contents of the array as long as it's \0 (nul) terminated...fprintf and the likes dump entire arrays all of the time.
char *p;
char string[12] = "A string";
for (p = &string[0]; *p != '\0'; p++)
fprintf(stdout, "%c", *p);
Is akin to
printf("%s", string);

Resources