Preference in parameters order for argc, argv [closed] - c

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 months ago.
Improve this question
Trivial question.
I have functions which manipulate the command line string, and I found easier to pass argc and argv to them, so that I delegate the treatment and I don't invent extra things before calling this functions.
myFunction(argc, argv, with, extra, things);
argc is a number (type int)
argv is a pointer to strings (type char*[])
But usually, functions that get a pointer and a size reclaim the pointer first and the size next.
Should I write all my functions like (argc, argv) or should I mimic the more usual (pointer, size) meaning (argv, argc) ?

Ignoring various subjective opinions on the matter.
There is one reason to put the size before the data in modern C, and that is because using VLA-type parameters using the passed size gives self-documenting code and possibilities of increased static analysis/compiler warnings
Examples:
void func (size_t size, int array[size]);
void parse_cmdline (int argc, char* argv[argc]);
In order to utilize this feature of (pointer-to) VLA, the size parameter must be declared to the left of the data parameter.
If you don't take advantage of this feature, then the order doesn't matter.

Related

can array of pointers point to variable sized strings? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I like to know is this code wrong. reasons pleasec
#include <stdio.h>
#include <string.h>
#include <malloc.h>
void func(char **c)
{
strcpy(c[0],"he");
strcpy(c[1],"h");
// memcpy(*c[2],"hell",sizeof("hell"));
// c[0]="hello";
//c[1]="hi";
}
int main()
{
char *arr[2];
arr[0]=malloc(sizeof(char[3]));
arr[1]=malloc(sizeof(char[2]));
func(arr);
printf("%s\n",arr[0]);
printf("%s\n", arr[1]);
return 0;
}
this line seems to allocate 3 * sizeof(char)
arr[0]=malloc(sizeof(char[3]));
this line seems to allocate 2 * sizeof(char)
arr[1]=malloc(sizeof(char[2]));
can array of pointers point to variable sized strings?
Yes.
is this code wrong.
func() is brittle in that it may exhibit undefined behavior depending on value of input.
No information about how many c[] may be indexed.
No information about how many bytes are available for strcpy(c[i], ....) to copy.
func() is OK in main() as it passes in a value that happens to not exceed the function limitations in OP's case.
func() is a wrong design in that is lacks parameters to guide its limitations and lacks null pointer error detection.

Array out of bounds in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to know how I can catch IndexOutofbounds in C.
I do not want my program to exit abnormally, I want to print a message for the user that clarify the error
how I can check for this case
char a[50];
fgets(a,200,stdin);
I need to exit the program and throw an error,and I do not need to change the 200 to use sizeof()
This is one of those things you simply cannot do in C. At least not without a lot of hazzle. You will have to keep track of such things yourself. So when you declare an array, then you will have to store the size and do something like this:
size_t size=1000;
int arr[size];
...
if(i>=size || i<0) {
// Handle error
} else {
// Do something with arr[i]
}
You can make abstractions and make it more Java-like with constructs like this:
struct array {
int *val;
size_t size;
};
int getVal(struct array array, size_t index) {
if(index>=array.size || index<0)
// Handle error
else
return array.val[index];
}
But if you are using constructs like that, chances are high that it might me a good idea to switch to another language instead.
If we look at your particular example:
char a[50];
fgets(a,200,stdin);
I'm sorry to say it, but it is impossible to make fgets throw an error in this situation.
First of all, C doesn’t have a structured exception handling mechanism. There’s no try...catch control structure in C (there is in C++ and C#, but those are different languages).
Secondly, C doesn’t do any bounds checking on array accesses - you, the programmer, are expected to do those checks yourself.

Pointers in C with arrays [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Here is the code:
#include <stdio.h>
int main ()
{
int c[4][4] = {{0,1,2,3},{4,5,6,7},{8,9,10,11},{12,13,14,15}};
int (*p)[3] = (int(*)[3])(c+2);
return 0;
}
Its interesting that it sets p to be:
{{8,9,10},{11,12,13},{14,15,-8224}}
I do not understand what is happening, especially in the 6th line of the code. Please help me!
int c[4][4] declares a 2D array with dimensions 4x4.
int (*p)[3] declares an array pointer to an array of 3 elements.
(int(*)[3])(c+2) invokes a pointer aliasing bug, by treating the address of c+2 as if there was an array of 3 elements there.
Pointer conversions between type int (*)[4] (the type of c+2) and type int(*)[3] are not safe. In practice, most compilers will likely give you some deterministic result from this code, but they are not required to do so. The program may as well crash and burn and then that's the programmer's fault.

Reversing a string (input parameter and return a string) [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have to answer this question:
Write a program which reverses the given string. (Input parameter and return string)
Does this question mean I have to take the input using command-line argument?
How will the return value work if I return a string to the main function?
Or is this just a misprinted question where I am supposed to make another function and return the value to some string in main function?
Does this Question means i have to take input using Command line argument?
It doesn't specify, but I would as it makes the task easier.
How will the return value work
Again, the question doesn't specify, so I would print it to the screen (printf).
Or is this just a misprinted question where I am supposed to make another function and return value to some string in main function?
Calling a function from the main function would be a great idea and could only receive credit in my opinion although not explicitly specified.
One thing is clear, they ask for a program and not just a function, but both would be best in my opinion.
The question means that you should implement a function that is a part of a program (probably a good idea to implement main to call/test the function). The function's' signature should be of the form:
char* reverse(char* str)
Although you could also implement (might be a little more difficult):
void inplace_reverse(char* str)
In my opinion, from the way the question is put there is no reason to believe you are to use command line arguments.
It means two things:
1- You must implement a function with the following signature:
char *reverse(const char *str);
2- You must implement a main() function which reverses the string passed as parameter:
int main(int argc, char *argv[]) {
char *str;
if (argc < 2) {
fprintf(stderr, "Usage: %s <string_to_reverse>\n", argv[0]);
return -1;
}
str = reverse(argv[1]);
printf("%s\n", str);
return 0;
}

Functions declared as pointers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I've seen certain functions declared in this way:
char* encipher(const char *src, char *key, int is_encode);
I don't understand this part:
char* encipher
What do the asterisks after the datatype mean?
This just means that the function returns a char *.
The asterisks after the data types mean that a pointer is expected, i.e.
char *src
means that src is a pointer to a char. Pointers are data types that contain addresses to instances of other data types, so a char* contains the address of a char. The first char* means that the function returns such a pointer.
But as others said, you may want to read a good textbook on C first.

Resources