int execle(const char *path, const char *arg, ..., char * const envp[]); - linux

int execle(const char *path, const char *arg, ..., char * const envp[]);
In the prototype of function above used in Linux, after variable arguments, they used named variable which is not possible in C language.
Because in C language in variadic function after variable argument we can not pass named argument then why in function given above they used named argument after variable argument (...).

The final argument is technically part of the variable argument list (...). It is just shown to provide the type and a name for the final argument, to make the documentation easier to understand. The current version of the manual page shows this argument in a comment to match the actual C prototype.

Related

How do I concatenate raw bits/hex values onto a String in Fortran?

I want to do something like this (C sample):
char str[] = "Hello\x90\x90\xcc\x00";
How?
The equivalent of this in Fortran would be:
character(*), parameter :: str = "Hello"//char(144)//char(144)//char(204)//char(0)
I made this a named (PARAMETER) constant here, but the expression for initializing would be the same in a normal assignment context. Standard Fortran doesn't allow the use of hex constants (such as Z'90') as an argument to CHAR, though many compilers support that as an extension.

How do you easily differentiate the use of * as a pointer, * as a de-reference operator, * as a multiplication operator in C?

The use of * is all so confusing especially for us new comers to C. I mean, how do you easily differentiate the use of * as a pointer, * as a de-reference operator, * as a multiplication operator? I see alot of different use of * online and it's quite confusing, especially regarding different positions of the * e.g. as in the sample code below. What positions of * are correct and advisable to use to avoid confusion with pointers, de-referencing and multiplication?
Code is just sample and may...infact will not compile.
int main(void){
int *size, length, width;
*size1= malloc(sizeof(int) * length);
*size2= (int *) malloc(width * sizeof(int) * width);
*size3= malloc(sizeof(int) *length);
printf("%d\n%d\n%d\n", size1,size2,size3);
return (0);
}
N.B: I'm new in C and pls don't refer me to useless links and ("supposedly") duplicates that doesn't answer exactly same question as this...pls allow others to air their views so we get maximum answers, none knows it all, u will be surprised that something new can pop-up, even for the so called C old-timers.
When you read C code, you must be able to distinguish definitions and expressions. A definition starts with a type optionally preceded by a storage class such as extern, static, typedef... and/or type qualifiers such as const and, volatile. A star in front of the identifier being defined indicates that this variable is a pointer, multiple stars indicate multiple levels of indirection.
In an expression, a star can occur as a binary operator, between operands, or as unary operator, in front of its operand. The binary operator * is the multiplication operator, whereas the unary * is the dereference operator.
As a rule of thumb, a * that follows an identifier or a postfix operator is a multiplication operator. Postfix operators are ++, --, indexing with [] and calling a function with optional arguments inside ().
The confusion comes from the conciseness of the C language, which allows the programmer to combine these different uses at will:
int x=0,*p=&x,n=*p**p;
This ugly line is indeed confusing. Let's first insert meaningful spaces to improve readability:
int x = 0, *p = &x, n = *p * *p;
Spacing is mostly unnecessary for the C compiler, but following simple rules increases readability for humans:
add a space on both sides of binary operators
add a space after , and ; except at the end of a line
do not insert a space between unary operators and their operand, except for sizeof.
indent code in a consistent manner, typically 4 spaces per level
The above line of code defines 3 variables x, p and n.
* p is a pointer to int, while x and n are int variables.
* x is initialized to 0
* p is initialized to the address of x.
* the initializer for n is the result of multiplying of the value pointed to by p by itself.
Defining multiple variables with different levels of indirection in the same definition is frowned upon because it is confusing and error prone. Rewriting the above definition on 3 lines is highly advisable:
int x = 0;
int *p = &x;
int n = *p * *p;

Bison simple postfix calculator using union with struct

Im making a simple calculator that prints postfix to learn bison. i could make the postfix part work ,but now i need to do assigments to variables (a-z) tex: a=3+2; should print: a32+=; for example. Im trying modified my working postfix code to be able to read a char too.
If I understand correctly, to be able to put different types in the $$ i need a union and to make the nodes i should use a struct because in my case 'expr' can be a int or a char.
This is my parser:
%code requires{
struct nPtr{
char *val;
int num;
};
}
%union {
int iValue;
char sIndex;
struct nPtr *e;
};
%token PLUS MINUS STAR LPAREN RPAREN NEWLINE DIV ID NUMBER POW EQL
%type <iValue> NUMBER
%type <sIndex> ID
%type <e> expr line
%left PLUS MINUS
%left STAR DIV
%left POW
%left EQL
%%
line : /* empty */
|line expr NEWLINE { printf("=\n%d\n", $2.num); }
expr : LPAREN expr RPAREN { $$.num = $2.num; }
| expr PLUS expr { $$.num = $1.num + $3.num; printf("+"); }
| expr MINUS expr { $$.num = $1.num - $3.num; printf("-"); }
| expr STAR expr { $$.num = $1.num * $3.num; printf("*"); }
| expr DIV expr { $$.num = $1.num / $3.num; printf("/");}
| expr POW expr { $$.num = pow($1.num, $3.num); printf("**");}
| NUMBER { $$.num = $1.num; printf("%d", yylval); }
| ID EQL expr { printf("%c", yylval); }
;
%%
I have this in the lex to handle the "=" and variables
"=" { return EQL; }
[a-z] { yylval.sIndex = strdup(yytext); return ID; }
i get and error
warning empty rule for typed nonterminal and no action
the only answer i found here about that is this:
Bison warning: Empty rule for typed nonterminal
It says to just remove the /* empty */ part in:
line: /* empty */
| line expr NEWLINE { printf("=\n%d\n", $2.num); }
when i do that i get 3 new warnings:
warning 3 nonterminal useless in grammar
warning 10 rules useless in grammar
fatal error: start symbol line does not derive any sentence
I googled and got some solutions that just gave me other problems, for example.
when i change:
line:line expr NEWLINE { printf("=\n%d\n", $2.num); }
to
line:expr NEWLINE { printf("=\n%d\n", $2.num); }
bison works but when i try to run the code in visual studio i get a lot of errors like:
left of '.e' must have struct/union type
'pow': too few arguments for call
'=': cannot convert from 'int' to 'YYSTYPE'
thats as afar as i got. I cant find a simple example similar to my needs. I just want to make 'expr' be able to read a char and print it. If someone could check my code and recommend some changes . it will be really really appreciated.
It is important to actually understand what you are asking Bison to do, and what it is telling you when it suggests that your instructions are wrong.
Applying a fix from someone else's problem will only work if they made the same mistake as you did. Just making random changes based on Google searching is neither a very structured way to debug nor to learn a new tool.
Now, what you said was:
%type <e> expr line
Which means that expr and line both produce a value whose union tag is e. (Tag e is a pointer to a struct nPtr. I doubt that's what you wanted either, but we'll start with the Bison warnings.)
If a non-terminal produces a value, it produce a value in every production. To produce a value, the semantic rule needs to assign a value (of the correct tag-type) to $$. For convenience, if there is no semantic action and if $1 has the correct tag-type, then Bison will provide the default rule $$ = $1. (That sentence is not quite correct but it's a useful approximation.) Many people think that you should not actually rely on this default, but I think it's OK as long as you know that it is happening, and have verified that the preconditions are valid.
In your case, you have two productions for line. Neither of them assigns a value to $$. This might suggest that you didn't really intend for line to even have a semantic value, and if we look more closely, we'll see that you never attempt to use a semantic value for any instance of the non-terminal line. Bison does not, as far as I know, attempt such a detailed code examination, but it is capable of noting that the production:
line : /* empty */
has no semantic action, and
does not have any symbols on the right-hand side, so no default action can be constructed.
Consequently, it warns you that there is some circumstance in which line will not have its value set. Think of this as the same warning as your C compiler might give you if it notices that a variable you use has never had a value assigned to it.
In this case, as I said, Bison has not noticed that you never use value of line. It just assumes that you wouldn't have declared the type of the value if you don't intend to somehow provide a value. You did declare the type, and you never provide a value. So your instructions to Bison were not clear, right?
Now, the solution is not to remove the production. There is no problem with the production, and if you remove it then line is left with only one production:
line : line expr NEWLINE
That's a recursive production, and the recursion can only terminate if there is some other production for line which does not recurse. That would be the production line: %empty, which you just deleted. So now Bison can see that the non-terminal line is useless. It is useless because it can never derive any string without non-terminals.
But that's a big problem, because that's the non-terminal your grammar is supposed to recognize. If line can't derive any string, then your grammar cannot recognize anything. Also, because line is useless, and expr is only used by a production in line (other than recursive uses), expr itself can never actually be used. So it, too, becomes useless. (You can think of this as the equivalent of your C compiler complaining that you have unreachable code.)
So then you attempt to fix the problem by making (the only remaining) rule for line non-recursive. Now you have:
line : expr NEWLINE
That's fine, and it makes line useful again. But it does not recognise the language you originally set out to recognise, because it will now only recognise a single line. (That is, an expr followed by a NEWLINE, as the production says.)
I hope you have now figured out that your original problem was not the grammar, which was always correct, but rather the fact that you told Bison that line produced a value, but you did not actually do anything to provide that value. In other words, the solution would have been to not tell Bison that line produces a value. (You could, instead, provide a value for line in the semantic rule associated with every production for line, but why would you do that if you have no intention of every using that value?)
Now, let's go back to the actual type declarations, to see why the C compiler complains. We can start with a simple one:
expr : NUM { $$.num = $1.num; }
$$ refers the the expr itself, which is declared as having type tag e, which is a struct nPtr *. The star is important: it indicates that the semantic value is a pointer. In order to get at a field in a struct from a pointer to the struct, you need to use the -> operator, just like this simple C example:
struct nPtr the_value; /* The actual struct */
struct nPtr *p = &the_value; /* A pointer to the struct */
p.num = 3; /* Error: p is not a struct */
p->num = 3; /* Fine. p points to a struct */
/* which has a field called num */
(*p).num = 3; /* Also fine, means exactly the same thing. */
/* But don't write this. p->num is much */
/* more readable. */
It's also worth noting that in the C example, we had to make sure that p was initialised to the address of some actual memory. If we hadn't done that and we attempted to assign something to p->num, then we would be trying to poke a value into an undefined address. If you're lucky, that would segfault. Otherwise, it might overwrite anything in your executable.
If any of that was not obvious, please go back to your C textbook and make sure that you understand how C pointers work, before you try to use them.
So, that was the $$.num part. The actual statement was
$$.num = $1.num;
So now lets look at the right-hand side. $1 refers to a NUM which has tag type iValue, which is an int. An int is not a struct and it has no named fields. $$.num = $1.num is going to be processed into something like:
int i = <some value>;
$$.num = i.num;
which is totally meaningless. Here, the compiler will again complain that iValue is not a struct, but for a different reason: on the left-hand side, e was a pointer to a struct (which is not a struct); on the right-hand side iValue is an integer (which is also not a struct).
I hope that gives you some ideas on what you need to do. If in doubt, there are a variety of fully-worked examples in the Bison manual.

Mismatch on sign: 'unsigned short' passed as _Param when some signed type is required in call to 'swprintf'

Mismatch on sign: 'unsigned short' passed as _Param when some signed type is required in call to 'swprintf'.
swprintf( buffer, L"%04d-%02d-%02d-%02d.%02d.%02d.%03d000",
sysTime.wYear, sysTime.wMonth, sysTime.wDay,
sysTime.wHour, sysTime.wMinute, sysTime.wSecond,
sysTime.wMilliseconds );
The members of the SYSTEMTIME structure are of type WORD. Reference.
The type WORD is a 16-bit unsigned integer (most likely unsigned short). Reference.
swprintf's "%d" specifier requires an argument of type int.
It's a bit odd that your compiler is warning you about this. If int is wider than short (which is common but not guaranteed), then an argument of type unsigned short, when passed to a variadic function like swprintf, will be prompted to int. (If int and short are the same width, an unsigned short will be promoted to unsigned int.)
You can avoid the problem by ensuring that all your arguments are actually of type int:
swprintf( buffer, L"%04d-%02d-%02d-%02d.%02d.%02d.%03d000",
(int)sysTime.wYear, (int)sysTime.wMonth, (int)sysTime.wDay,
(int)sysTime.wHour, (int)sysTime.wMinute, (int)sysTime.wSecond,
(int)sysTime.wMilliseconds );
For functions with a fixed number and types of parameters, arguments of arithmetic type (signed integer, unsigned integer, or floating-point) are implicitly converted to the correct type. For variadic functions like printf and swprintf, the declaration of the function doesn't provide this information (and the format string can even be a variable, leaving the compiler completely in the dark about what types are expected), and so you have to be much more careful to ensure that you're passing arguments of the correct type.
(The "some signed type" warning is very odd. Some warning is warranted, since passing an unsigned short with a "%d" format is not portable, but the promoted argument must be specifically of type int. Passing a long or long long, each of which is "some signed type", would be equally incorrect.)

how to use a variable in parameters

i need to know how to use a variable in parameters
PROCESS is initalized as
string PROCESS in the class parameters.
Process::GetProcessesByName("")
which gives me an error when i replace the string with a variable
Process::GetProcessesByName(PROCESS)
The error i get back is:
No instance of overloaded function "System::Diagnostics::Process::GetProcessByName" Matches the argument list argument types are (std::string)
I think GetProcessesByName may take a parameter as LPSTR or char const *, but not a std::string,
try use
Process::GetProcessesByName(PROCESS.c_str());
or if your PROCESS is defined as std::string PROCESS;,
String^ process_name = gcnew String(PROCESS.c_str());
Process::GetProcessesByName(process_name);

Resources