C++ Assigning a value to char *argv[] from inside a program - input

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.

Related

Why readline() input-echoing in console?

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.

C Blocks indirection and misalignment

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 to write a program for addition of two numbers in google test?

I am working with visual studio 2005...
Can anybody tell me how to write most basic program for addition of two numbers in google test??
I have gone through almost all the references given...but somehow didnt understand how to get started?
The following would be about as basic as it can get:
#include "gtest/gtest.h"
int GetSum(int a, int b) { return a + b; }
TEST(MathsFunctions, GetSum) {
ASSERT_EQ(3, GetSum(1, 2));
}
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
You need to provide the path to gtest's include directory and link to gtest.lib

How is the CodeRunner Program Input field used?

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);
}
}

Learning Objective-C and I'm having problems with this snippet of code: (scanf)

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