What is the right way to explain this code? [closed] - c

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
Shouldn't st be a array of pointers to char rather than a pointer to char? I do not understand how the latter for loop prints the value?
int main(void)
{
char temp[256];
char *st;
for (int i = 0; i < 3; i++)
{
scanf("%s", temp);
st= strdup(temp);
}
for(int i=0;i<3;i++)
{
printf("%s",st);
}
}

You probably want this:
#include <stdio.h>
#include <string.h>
int main(void)
{
char temp[256];
char *st[3]; // array of three pointers to char
for (int i = 0; i < 3; i++)
{
scanf("%255s", temp); // prevents potential buffer overflow
st[i] = strdup(temp);
}
for(int i = 0; i < 3; i++)
{
printf("%s\n", st[i]);
free(st[i]); // free strduped memory
}
}
This program displays:
./a.out
11
22
33
11
22
33
Whereas your program displays
./a.out
11
22
33
33
33
33
this is because:
char *st; // in your prog. you only declare one pointer
for (int i = 0; i < 3; i++)
{
scanf("%s", temp);
st= strdup(temp); // here you overwrite the st pointer loosing
// the string strduped in the previous run of the loop
}

Related

Splitting string sequence into integers [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 6 years ago.
Improve this question
How do you split a string:
char *mystring = "12345"
into an integer array which looks like this:
[1, 2, 3, 4, 5]
I have tried something like the code below, but I'm not entirely sure if it's reliable, and I think it will be easy to break. This is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(void) {
char *mystring = "12345";
int string_size, i, length;
string_size = strlen(mystring);
int values[string_size];
for (i = 0; mystring[i] != '\0'; i++) {
values[i] = mystring[i] - 48;
}
length = sizeof(values)/sizeof(*values);
for (i = 0; i < length; i++) {
printf("%d ", values[i]);
}
return 0;
}
Which outputs:
1 2 3 4 5
Is there a more C like way I can do this?
The odd thing I see, which isn't itself a problem, is that you calculate the length of the string/array three different ways:
string_size = strlen(mystring);
for (i = 0; mystring[i] != '\0'; i++) {
length = sizeof(values)/sizeof(*values);
where just one method is sufficient:
#include <stdio.h>
#include <string.h>
int main(void) {
char *mystring = "12345";
size_t length = strlen(mystring);
int values[length];
for (int i = 0; i < length; i++) {
values[i] = mystring[i] - '0';
}
for (int i = 0; i < length; i++) {
printf("%d ", values[i]);
}
printf("\n");
return 0;
}
You can replace 48 with '0' for readability.
You can change all loops to loop until string_size like the first one, no need to change the method for each loop.
And finally if you're going to return that array anywhere outside of local function, you should probably malloc() it rather than use a local/stack variable.
But otherwise, it's pretty simple and it works.

Unknown error regarding strings and stdio.h library [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 7 years ago.
Improve this question
I'm receiving an uknown error found in the stdio.h library.
Please can someone check it at tell me what is wrong with the code (But I thing it should work fine).
P.S. I'm new here so please don't blame me if this is a bad question.
Code:
#include <stdio.h>
#include <stdlib.h>
// Conversion from a number to a string
char *i2s(int broj);
int main()
{
char string1;
int br, n;
do
{
printf("How much numbers?\n -"), scanf("%d", &n);
} while (n < 1);
for (int i = 0; i < n; i++)
{
printf("\nbr = "), scanf("%d", &br);
string1 = *i2s(br);
printf(" %s", string1);
}
free(string1);
getch();
return 0;
}
char *i2s(int broj)
{
char *pom;
int z=0,br=0,p;
if (broj < 0)
{
z = 1;
broj = -broj;
}
p = broj;
do
{
br++;
p /= 10;
} while (p);
pom = (char *)calloc(br + 1 + z, sizeof(char));
if (z)
pom[0] = '-';
do
{
pom[--br + z] = '0' + broj % 10;
} while (broj /= 10);
return pom;
}
char string1;
free(string1);
string 1 is not a pointer.
Also with the following section you overwrite string1 everytime you run through the loop. that way you have no pointer to free() the memory that you allocate inside your function unless you do it inside the loop.
for (int i = 0; i < n; i++)
{
printf("\nbr = "), scanf("%d", &br);
string1 = *i2s(br);
printf(" %s", string1);
}

C can not loop just one time [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
code here:
#include <stdio.h>
#define ROWS 6
#define CHARS 10
int main(void)
{
int row;
char ch;
for(row = 0; row < ROWS; row++)
{
printf("%d\n", row);
for(ch = 'A'; ch < ('A' + CHARS); ch++)
printf("%c", ch);
printf('\n');
}
getchar();
return 0;
}
output here:
0
ABCDEF
i think the output similar like this:
0
ABCDEF
1
ABCDEF
2
ABCDEF
3
ABCDEF
4
ABCDEF
5
ABCDEF
question is why loop just one time.
"ABCDEF" contains 6 chars, so you need to change
#define CHARS 10
to
#define CHARS 6
Also, the printf takes a string, so you should use "\n" instead of '\n'.
#include <stdio.h>
#define ROWS 6
#define CHARS 6
int main(void)
{
int row;
char ch;
for(row = 0; row < ROWS; row++)
{
printf("%d\n", row);
for(ch = 'A'; ch < ('A' + CHARS); ch++)
printf("%c", ch);
printf("\n"); // Should use double quotes here
}
getchar();
return 0;
}

Why does my tolower call not execute? [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 8 years ago.
Improve this question
char * abc = "ABC";
int i;
printf("%s\n", abc);
for (i = 0; i < strlen(abc); i++)
{
abc[i] = tolower((int) abc[i]); //Error at this line
}
printf("%s\n", abc);
The line where I call tolower does not execute?
char * abc = "ABC"; defines a string literal
which cannot be changed during runtime.
Use char abc[] = "ABC";
edit: Maybe you "can" change it in some cases,
but there is no guarantee for anything.
It could work, it could crash,
or doing other strange things with your program
which are not immediately recognizable.
The abc points to a constant string, and because it is read-only, you can not change the value directly. There are 2 methods.
(1)You can allocate the memory in the stack:
char abc[] = "ABC";
int i;
printf("%s\n", abc);
for (i = 0; i < strlen(abc); i++)
{
abc[i] = tolower((int) abc[i]); //Error at this line
}
printf("%s\n", abc);
(2)You can also allocate the memory in heap, and the code is like this:
int len = strlen("ABC");
char *p = malloc(len + 1);
strcpy(p, "ABC");
printf("%s\n", p);
for (i = 0; i < len; i++)
{
p[i] = tolower((int) p[i]); //Error at this line
}
printf("%s\n", p);

How to extract a part of a char array in C? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I receive 10 bytes (010830FFFFFFFFFF0304) in hexa on terminal, and I need to write only a part of this array (This 5 bytes FFFFFFFFFF) in another char array.
HereĀ“s the code:
#include <stdio.h>
void main(){
char buffer[]; // buffer size in bytes
int i; // counter
while(true){
for(i=0; i<buffer; i++){
buffer[i] = getc();
}
}
}
How to extract this part of array?
General fixup:
I declared an actual size for buffer (10), where you did not have a size.
I declared an array for your answer (middle), where you did not have such a space.
I loop 10 times, instead of the non-sense comparison i<buffer
I removed the outer while(true), which would never terminate.
I memcpy from the input buffer to the answer, for 5 characters.
Your question is very unclear and imprecise, so I can only guess at what you intended.
void main()
{
char buffer[10]; // buffer size in bytes
char middle[5];
int i; // counter
for(i=0; i<10; ++i){
buffer[i] = getc();
}
memcpy(middle, &buffer[3], 5*sizeof(char));
for(i=0; i<5; ++i)
{
printf("%x",middle[i]);
}
printf("\n");
}
as it seems you have a while(true) loop and all you need to do is just create another char array and put the needed chars in it:
#include <stdio.h>
void main()
{
char buffer[10]; // buffer size in bytes
char smallBuf[5]; // buffer size in bytes
int i; // counter
for(i=0; i<10; i++)
{
buffer[i] = getc();
}
for(i=0; i<5; i++)
{
smallBuf[i] = buffer[i + 3];
}
}
#include <stdio.h>
void main(){
char buffer[64]; // buffer size in bytes
char result[10];
int i; // counter
while(1){
for(i=0; i<10; i++){
buffer[i] = getc(stdin);
}
for(i=0; i<5; i++){
result[i] = buffer[i+3];
}
result[i] = 0; // end string
printf("%s\n",result);
}
}

Resources