int main(int argc, const char * argv[])
{
const char* input = readline(NULL);
NSLog(#"%d", atoi(input));
return 0;
}
I was trying to enter a sequence of characters in console, but whenever I typed the one key, the same char will following immediately after the char which I just typed. (For example, I typed '1' in console, the console will display "11") Moreover, when I try to delete the entered char using delete-key. The up-side-down question mark will appear, but inside the char* the last char has been deleted?
Why all these happened?
This is expected behavior as of Xcode 5. You can run it outside Xcode in the terminal and it should behave normally.
Related
The customary way of passing values through argv in this declaration
int main(int argc, char* argv[]) {
is to pass it from your console through argv array into a program. I came across a situation where I wanted to achieve something different as follows. The program has the usual
int main(int argc, char* argv[]) {
declaration and inside my program I have a
function void foo(argv[1], 0).
Here is the layout and flow of my program's logic:
program ggg.cpp
:
void foo(char* x, int y) {
:
}//end foo
:
int main(int argc, char* argv[]) {
:
foo(argv[1], 0);
:
}//end main
(1) My normal method. Normally, when I compile the ggg.cpp program and my console input is:
ggg.exe . //the second input is a dot "."
the program runs fine (because argv[1] = '.' inside the program)
(2) However, I wanted to get innovative and execute the program as:
ggg.exe //here, the dot is not included
so that inside the program, I can assign the dot to argv[1] but I kept getting errors - no matter how I try other variations of the assignment statement.
By the way, I tried the assignment statement
argv[1]='.';
inside the main program (in method (2)) but it didn't work. I got an error message.
Background Information. I used to be very good in programming in my hay days but now I have forgotten a lot of things because I left coding for a very long time to do other things.
I write a small console program in objective-c. It need to use the scanf method to receive the number.When I enter a character, it will make a mistake.So I try to solve it,but it has entered a cycle of death! See the following code, to help me solve it, thank you very much!
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
#autoreleasepool {
int num1 = 0;
NSLog(#"Please input number:");
while (!scanf("%d", &num1)) {
fflush(stdin);
NSLog(#"Input error,just input number:");
}
}
return 0;
}
The documentation for fflush states:
The function fflush() forces a write of all buffered data for the given output or update stream via the
stream's underlying write function. The open status of the stream is unaffected.
and you are trying to flush input. Try fpurge:
The function fpurge() erases any input or output buffered in the given stream.
Also do not write !scanf(...). Check the documentation, this function returns an integer, not a boolean, and the value could be positive or negative (look up the definition of EOF). A negative value indicates an error, but the ! operator will yield false and your code would not ask for new input.
If successful scanf returns the number of items successfully parsed, check for that.
The documentation for all these functions is available via Xcode or the man command.
HTH
The following code throws an EXC_BAD_ACCESS error (specifically a general protection fault error) and I would like to know why you cannot misalign a block pointer and execute it.
#include <stdio.h>
int main(int argc, const char * argv[])
{
void (^blocky)() = ^{
printf("Hello!\n");
printf("Hello Again!\n");
};
blocky = *(&blocky+1);
blocky = *(&blocky-1);
blocky();
return 0;
}
But the following works:
#include <stdio.h>
int main(int argc, const char * argv[])
{
void (^blocky)() = ^{
printf("Hello!\n");
printf("Hello Again!\n");
};
blocky = *(&blocky+1-1);
blocky();
return 0;
}
Edit (answer to misaligning code blocks):
If you treat a block like a structure, you can find that the value that points to the executable code in memory is offset 16 bytes from the start of the block and is 8 bytes long.
You are able to change this value effectively pointing execution to another place in memory. Generally, this will crash.
Assuming you know the specific address in memory for another piece of executable code, you may direct it there.
Why this is useful:
It isn't. Never do this. Really. Never.
The pointer manipulation in the first example is wrong. Try this:
#include <stdio.h>
typedef void (^blocky_t)();
int main(int argc, const char * argv[])
{
blocky_t blocky = ^{
printf("Hello!\n");
printf("Hello Again!\n");
};
printf("blocky=%p\n", blocky);
blocky = (blocky_t)((char *)blocky + 1);
printf("blocky=%p\n", blocky);
blocky = (blocky_t)((char *)blocky - 1);
printf("blocky=%p\n", blocky);
blocky();
return 0;
}
$ clang -o blocky blocky.c
$ ./blocky
blocky=0x10574d040
blocky=0x10574d041
blocky=0x10574d040
Hello!
Hello Again!
When I ran your code, I got:
blocky=0x10e0ba040
blocky=0x7fff51b46c10
blocky=0x1300000000
Where:
The first address is within the __TEXT segment of the program.
The second address is near the stack.
The third is who-knows-where.
Your question really has nothing to do with blocks. You're just manipulating pointers to local variables in a way that doesn't make sense.
First, you never use the block pointer that you assign to blocky. You take the address of the local variable blocky on the stack, and then add one word to it, and dereference it. Depending on the architecture, the stack probably grows down, which means this is before all the variables on the stack frame, and is probably the return address of the current stack frame. Or it may be something else. You then assign this value to blocky.
Then, you take the address of the local variable blocky on the stack again, and then subtract one word from it, and dereference it. Again, assuming the stack grows down, this might be past the end of the current stack frame, which would be garbage. You then assign this value to blocky. You then try to run this as a pointer to a block. Of course this doesn't work.
In the second piece of code, you take the address of the local variable blocky on the stack again, and then add and subtract one word from it (which of course is the pointer to the local variable blocky again), and dereference it (which is the value of blocky), and assign it to blocky. This operation does nothing.
How is the Custom Run sheet Program Input field used in CodeRunner say for a C or Objective-C program?
The text entered in the Program Input text box will be sent to your program/script through the standard input. You can access the input in the same way that you would access the standard input using your language of choice.
In C, you'd use the standard fread, fgets, fgetc etc functions. The following example will echo the supplied text in the Program Input to the console:
#include <stdio.h>
int main(int argc, char *argv[]) {
char str[80];
while(fgets(str, sizeof(str), stdin)) {
printf("%s", str);
}
}
I'm learning Objective-C and I'm having problems with this snippet of code: (scanf)
int main (int argc, const char * argv[])
{
#autoreleasepool {
double xnumber;
scanf("%1f",&xnumber);
printf("%f",xnumber);
}
return 0;
}
When I run this simple program, I cannot set the value of xnumber to anything. It alway returns zero no matter what I typed. This basic program it is just an input needed for many exercises... Can someone help me?
Use the "l" modifier to indicate you're reading a double
scanf("%lf", &xnumber);
scanf("%1f",&xnumber); you read a double,so change %1f to %lf.
you can read this http://www.dgp.toronto.edu/~ajr/209/notes/printf.html