I am reading an article about code obfuscation in C, and one of the examples declares the main function as:
int main(c,v) char *v; int c;{...}
I've never saw something like this, v and c are global variables?
The full example is this:
#include <stdio.h>
#define THIS printf(
#define IS "%s\n"
#define OBFUSCATION ,v);
int main(c, v) char *v; int c; {
int a = 0; char f[32];
switch (c) {
case 0:
THIS IS OBFUSCATION
break;
case 34123:
for (a = 0; a < 13; a++) { f[a] = v[a*2+1];};
main(0,f);
break;
default:
main(34123,"#h3eglhl1o. >w%o#rtlwdl!S\0m");
break;
}
}
The article: brandonparker.net (No longer works), but can be found in web.archive.org
It's the old style function definition
void foo(a,b)
int a;
float b;
{
// body
}
is same as
void foo(int a, float b)
{
// body
}
Your case is same as int main(int c,char *v){...} But it's not correct.
The correct syntax is : int main(int c, char **v){...}
Or, int main(int c, char *v[]){...}
EDIT : Remember in main() , v should be char** not the char* as you have written.
I think it's K & R C style.
It is a pre-ANSI C syntax for function declaration. We don't use it anymore. It is the same as:
int main(int c, char *v)
Related
Why is
"void (*parse(char *op))(int, int)"
written like this? (main added to give use case), it gets called from a pointer "fun" with argv[2] without the ints (line 18)... and then again as "fun" with the ints (line 21)?
void (*parse(char *op))(int, int)
{
if (!strcmp(op, "+"))
return(other stuff...);
else
return (0);
}
int main(int argc, char *argv[]){
int a;
int b;
void (*fun)(int, int);
if (argc == 4){
a = atoi(argv[1]);
fun = parse(argv[2]);
b = atoi(argv[3]);
if (fun)
fun(a, b);
else
return(0);
}
return(0);
}
How does it technically work, and it is just a showoff with a simpler way to write it or is this the only correct grammar?
parse() is a function that returns a function pointer. You can use typedef with the type of the returned function to make it look a lot nicer, though:
#include <string.h>
#include <stdlib.h>
// parser_func describes a function that takes two ints and returns nothing
typedef void (*parser_func)(int, int);
// Like this one.
void somefunc(int a, int b) {}
// And parse() is a function that takes a char* and returns a
// pointer to a function that matches parser_func.
parser_func parse(char *op) {
if (!strcmp(op, "+"))
return somefunc;
else
return NULL;
}
int main(int argc, char *argv[]) {
int a;
int b;
parser_func fun; // Function pointer
if (argc == 4) {
a = atoi(argv[1]);
fun = parse(argv[2]); // Assign a function to fun
b = atoi(argv[3]);
if (fun) {
fun(a, b); // And call it if it's not null
}
}
return 0;
}
Why is void (*parse(char *op))(int, int) written like this?
Because that is the syntax for returning a pointer to function. The return type is void (*)(int, int) which is a pointer to function that returns void and accepts two int arguments.
Usually, type aliases are used to make pointers to functions more readable:
typedef void operation(int, int);
using operation = void (int, int); // equivalent C++ alternative
operation* parse(char *op);
Can you guys help me on function m? The idea is to printf the "tab", but i don't understand what is wrong
#include <stdio.h>
#define MAXL 50
#define MAXC 50
unsigned int linhas;
unsigned int colunas;
int segC [MAXL];
int segL [MAXC];
char tab[MAXL][MAXC];
void c (){
int l,c,temp;
scanf("%d %d",&linhas,&colunas);
for (l=0;l<linhas;l++){
scanf("%d[^'']",&temp);
segC[l]=temp;
}
for (c=0;c<colunas;c++){
scanf("%d[^'']",&temp);
segC[c]=temp;
}
for(l=0;l<=linhas;l++){
for(c=0;c<colunas;c++){
scanf("%c",&tab[l][c]);
}
}
}
char m (linhas,colunas,segC,segL,tab){
int l,c;
int tempi;
char tempc;
for(l=0;l<=linhas;l++){
for(c=0;c<colunas;c++){
printf("%c",tab[l][c]);
}
tempi=segL[l];
printf("%d\n",tempi);
}
for(c=0;c<colunas;c++){
tempi=segC[c];
printf("%d",tempi);
}
printf("\n");
}
char h (int line){
}
int main (){
c();
//m(linhas,colunas,segC,segL,tab);
}
Rewrite the function like this:
char m() {
/* ... */
}
You do not need to provide global variables as arguments to a function; in fact, the local function parameters shadow the global variables.
Finally, avoid omitting parameter and variable types; that is at the very least deprecated or even illegal as of C99 (omitted types default to int which is causing the problem here.)
Better yet, declare them as local variables in main() and pass them by pseudo-reference to both m() and c():
char m( unsigned int linhas, unsigned int colunas, int **segC, int **segL, char ***tab ) {
/* ... */
}
Pass the address of segC, segL, and tab when calling.
You're missing variable types:
char m (linhas,colunas,segC,segL,tab)
On MSVC and gcc (GCC) 4.8.3 20140911 the following compiles and runs just fine:
#include <stdio.h>
int func(int a, int b){
return 0;
}
int main(void){
int (*funcPointer)(int a, int b);
funcPointer = func;
printf("funcPointer = %p\n", funcPointer);
return 0;
}
Is such behaviour well-defined, or is it non-standard and it's actually illegal for function pointer types to have named parameters (i.e. names as well as types in their parameter list)?
You can have a parameter in your function pointer. It is totally valid. The parameter list matches that of the function being called and the names is just optional.
It can also be written as
int (*funcPointer)(int,int);
I mean
int (*funcPointer)(int a, int b);
This is valid and you can verify the same by calling
int res = funcPointer(3,4);
and returning
int func(int a, int b){
return a+b;
}
It's perfectly legal. The names a and b in funcPointer are not used for anything, but they are permitted. You could use any (legal) names you want, they don't matter at all.
A question in the answer:
Why does this compile and what does it mean?
int func(int a) { return a; }
int main(int argc, char **argv)
{
int(*a)(int x(float)) = func;
printf("%d\n", a(1));
return 0;
}
So I have written this code and I wanted to ask is it possible to write main in the first place before other things?
#include <stdio.h> // Standard Ein-/Ausgabefunktionen
#include <at89c51cc03.h> // CC03er-Grundregister
#define CS_LCD 0xffb8
xdata unsigned char eak_io #0xff80;
xdata unsigned char DIS_IR_W #CS_LCD+0x0;
xdata unsigned char DIS_DR_W #CS_LCD+0x1;
xdata unsigned char DIS_IR_R #CS_LCD+0x2;
xdata unsigned char DIS_DR_D #CS_LCD+0x3;
void init_schnittstelle(void)
{
SCON=0x52; // Initialisierung
TMOD |=0x20; // Timermodus 8-bit auto-reload
TH1=0xfa; // 4800 Baud
TR1=1;
}
void ms_warten(unsigned int multiplikator)
{
unsigned int i,j;
for(i=0;i<multiplikator;i++)
{
for(j=0;j<123;j++);
}
}
void dis_ready(void)
{
while ((DIS_IR_R & 0x80)!=0);
}
void init_lcd(void)
{
unsigned char i;
for (i=0; i<=2; i++)
{
DIS_IR_W=0x30;
ms_warten(10);
}
// Function Set: DL=1, N=1, F=0
dis_ready();
DIS_IR_W=0x38;
// Display ON/OFF: D=1, C=1, B=0
dis_ready();
DIS_IR_W=0x0c;
// Entry Mode Set: I/D=1, S=0
dis_ready();
DIS_IR_W=0x06;
}
void dis_clear(void)
{
dis_ready();
DIS_IR_W=0x01;
}
void dis_csr_set(unsigned char z, unsigned char s)
{
unsigned char csr_pos;
switch (z)
{
case 0 : csr_pos=s;
break;
case 1 : csr_pos=s+0x40;
break;
case 2 : csr_pos=s+0x14;
break;
case 3 : csr_pos=s+0x54;
break; }
dis_ready();
DIS_IR_W=(csr_pos | 0x80);
}
void dis_text(unsigned char csr, unsigned char z, unsigned char s, char *a)
{
unsigned char i;
if (csr==1) dis_csr_set(z,s);
i=0;
while(a[i]!=0)
{
dis_ready();
DIS_DR_W=a[i];
i++;
}
}
void main(void)
{
char aktuellerWert;
init_schnittstelle();
init_lcd();
while(1)
{
RI = 0;
while(!RI);
if(SBUF != aktuellerWert)
{
aktuellerWert = SBUF;
switch(aktuellerWert)
{
case 'O': dis_clear();
dis_text(1, 1, 2, "blabla");
dis_text(1, 2, 1, "blabla");
dis_text(1, 3, 3, "blabla");
break;
case 'G': dis_clear();
dis_text(1, 1, 2, "blabla");
dis_text(1, 2, 1, "blabla");
break;
case 'R': dis_clear();
dis_text(1, 1, 2, "blabla");
dis_text(1, 2, 1, "blabla");
break;
}
}
}
}
So I would like to write main method before #define, o that it would be more or less in the first position.
Thanks!
The compiler has to know just a few things about the functions that you use in your code before the are called. The actual implementation/definition of the function is not needed, only a declaration (a function prototype) is needed before calling. This can be done in two ways:
In a header file. Use this method if you want to use your functions in multiple C files.
Anywhere in the C source file (preferably in the beginning). These functions are limited to file scope, i.e. they are only available for use in the C source where they are declared.
A function prototype looks like this:
return_type function_name(type_t param1, type_t param2);
For example:
int sum(int a, int b);
Would declare the function sum, telling the compiler that
A function named sum exists somewhere
The function takes two integers as parameters
The function returns an integer.
At this point, the compiler has no idea how the function is implemented. However, since the compiler knows that it exists and what it looks like, it will compile your code just fine.
Here is a short example using your code:
#include <stdio.h> // Standard Ein-/Ausgabefunktionen
#include <at89c51cc03.h> // CC03er-Grundregister
// Function prototypes for functions used in main() are here, now the compiler
// is aware of them
void init_schnittstelle(void); // Note the semicolon
void init_lcd(void);
// I didn't include the prototype for the function ms_warten(), since the main()
// Doesn't use it directly. Declatring it beforehand wouldn't hurt, though.
int main()
{
// Your code here
}
#define CS_LCD 0xffb8 // This isn't used by main() either, so the compiler
// doesn't needto know about it before the
// main() fucntion.
xdata unsigned char eak_io #0xff80;
xdata unsigned char DIS_IR_W #CS_LCD+0x0;
xdata unsigned char DIS_DR_W #CS_LCD+0x1;
void init_schnittstelle(void)
{
// Your code here
}
void ms_warten(unsigned int multiplikator)
{
// Your code here
}
There is something called a function declaration. As opposed to a function definition like
int foo(int x)
{
return x + 42;
}
which tells the compiler what a function does, a function declaration tells the compiler how to invoke a function. This would be a valid function declaration for foo:
int foo(int x);
Notice the lack of braces and the trailing semicolon. Declaring a function is sufficient for the compiler to know how to call it. So if you declare all the functions invoked by main() up front, you can define the main function first. Here is an example of what the might look like:
void init_schnittstelle(void);
void ms_warten(unsigned int multiplikator);
void dis_ready(void);
void init_lcd(void);
void dis_clear(void);
void dis_csr_set(unsigned char z, unsigned char s);
void dis_text(unsigned char csr, unsigned char z, unsigned char s, char *a);
void main(void)
{
/* Code hier */
}
The customary order for the things in a program is this:
feature test macros
#include directives
#define directives
type and enumeration declarations
function declarations
variable definitions
function definitions
Notice that there is a bunch of stuff before you get to define main(), but you can still make main() the first function defined.
I have a C++ program that shows an error:
too few arguments to function void split(char*, char**, int, int, int*)
Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
void split(char* lin, char** word, int i, int w, int* c);
int main() {
char line[80] = "myline";
int n = 5;
char **word;
split(line, word, 1, 1); //Error is here.
return 0;
}
void split(char* lin, char** word, int i,int w, int* c)
{
//statements
}
Can anyone tell whats wrong?
The function split takes 5 arguments and no default argument. You try to call it with 4 arguments. That wont work.
The last two times you call split() you're calling it with only 4 arguments, as in one too few. If you'd like you can define it for 4 arguments as well, but currently this is not the case