C char array storing a variable [closed] - c

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 would like to store 2 variables into char array, and print our the first one as shown below.
const char *a[2];
a[0] = getCapital(bufferStore); //"Australia"
a[1] = getCurrencyCode(bufferStore); "9876.00"
printf("%s", a[0]);
However, I did not get any output. The code of getCapital and getCurrencyCode should be redundant here. The main thing I want to find out is how I can print out "Australia". I'm new to C language and pointers are really hard to understand, and my assignment is due in 2 hours. Any help will be greatly appreciated!

The file stdout, which is what printf writes to, is by default line buffered. That means everything you write to it is buffered, i.e. stored in memory, and is flushed (and actually printed) when you print a newline.

Related

How do I add spaces in front of integer array parameters? [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
I have an array like this
int numbers[]={5,6,5,8,9,1,-6516,8,811,981,981};
and I need to print them to screen with spaces in front of them so the total number of characters printed will be 4. si the number 5 will be printed as 3 spaces and 5 5 the number 811 will be 811 and so on.
As was previously mentioned in comments, this is something you can do with printf. I'm reluctant to write the code because a) it looks like homework and b) you'll have this nailed once you've read up on printf. (You'll need to put that printf in a for-loop to go thru the array, for sure, so read up on for as well if you need to.)
Some good resources for printf are whatever textbook you're using, plus
https://www.tutorialspoint.com/c_standard_library/c_function_printf.htm
https://www.dummies.com/programming/c/how-to-format-with-printf-in-c-programming/
http://www.cplusplus.com/reference/cstdio/printf/ (a reference, not a tutorial)

Count amount of words, characters, newlines in 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
I know it is not recommended to post code as an image, since I am getting a formatting error when trying to post code on here. Anyway, I am trying to write a simple C program to count number of words, characters, newlines using do while loop. However, the output is not as expected. Please help!
Instead of a do-while, you should try using while. Since your code starts off by checking whether a is any of your if cases, it goes to the else case, and increments the New line variable. If possible, could you share the output screen.

Why strcmp seems not to work with a string returned by strtok? [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 3 years ago.
Improve this question
So, I have some code like:
char* t2 = strtok(NULL, " \t\n,");
char regN[3];
strcpy(regN, t2);
Register* rt = getRegister(regN);
I checked several times the value of regN by printing is $t0 but the function returns NULL.
However when I tried getRegister("$t0") it returns the correct value.
The only reason I can think for this is if strtok() returns something other than the predicted value. Yes, the next token is $t0. I checked this as well.
Any suggestions or ideas what is going wrong here?
Three char long string must be stored in the 4 char array as it also needs the space for terminating zero. Your array is 3 chars long, so you have the Undefined Behaviour here

Weird issue in 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 6 years ago.
Improve this question
thanks for your help.
I'm trying to similate a simplified version of ARM, and I have a very weird error in c http://pastebin.com/3XRdngty .
I don't understant why in the function executer_code(),
the for doesn't work ...
I mean it should be looping untill the variable "i" is equal to the variable nombre_instruction, but it turns out that the variable "nombre_instruction" is the right value the first time it goes in the for, but the second time it doesn't go in the for because its value changed to 0, I search on the internet if someone has the same kind of error, and i didn't find anything.I reread my code but still I cannot figure out why it does this, 3 hours has passed already.
And thank you again for you help :)
This is taken from your code:
char *compar;
if(i==0){
sprintf(&compar,"%c%c%c%c",code[0],code[1],code[2],code[3]);
}
The problem here is that you declare compar as a pointer to char, but it is uninitialized. So, it has an undefined value. When you fill it with sprintf, you just write somewhere in the memory, and apparently, you write over the variable nombre_instruction.
Solution:
char compar[200];

Converting a character into a space in C arrays [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
So lets say I have received a message that resembles the following
"L2N5*8R10!11T0A1K3Y14#4W7O6O9C12R13"
and I am expected to sort out the characters in accordance to the numbers succeeding them and change the characters that are not letters into a space. I have no problem doing the sorting out part, I am only having a trouble while trying to write a function that will change those characters into space.
The out put should be something like this
TALK NOW OR CRY
but I am getting
TALK#NOW*OR!CRY
Can anyone help me figure out what my function should look like so that I can be able to change the characters into space??
Unless you show your code, we'll only be able to guess!!
However, as a general suggestion, I would recommended, you should check each entry against isalpha(). In case you got a return value of 0, replace the entry with a .

Resources