Sorting Number Compiler Error [closed] - c

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 8 years ago.
Improve this question
I am trying to sorting a list of numbers using radixsort. But ran into a compiler
problem that I cannot solve after multiple tries.
I have two pointers one at the back of the list and the other in front.
base is the the number of buckets I am using for radixsort.
struct listnode **front,**back;
front = malloc(sizeof(*front) * base);
back = malloc(sizeof(*back) * base);
Error that I am getting is:
invalid conversion from void* to listnode**[-fpermissive]
Thanks in advance for the help.

It looks like you're using a C++ compiler to compile your C program. Either don't do that, or add a typecast to the return value of the malloc() calls.

The code works fine as a C program. Save the file with a .c extension an then compile it...
Still, if you want to compile it as a C++ program, you need to do a cast as
struct listnode **front,**back;
front = static_cast<listnode**> (malloc(sizeof(*front) * base));
back = static_cast<listnode**> (malloc(sizeof(*back) * base));
The reason is that C++ is a strongly typed language and does not allow the type of conversions between pointers like C does. You have to explicitly specify such a cast in C++. Implicit casts between pointers is a source of hard find bugs and thus not supported in C++.

Related

How to access .text section using a function pointer 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 5 years ago.
Improve this question
I am trying to inject executable code in memory using C. The executable should be able to read it's own code and copy it to some other location referenced by a pointer. The general template code is as follows
int
main(){
int (*main_ptr)();
main_ptr=main; // make the pointer point to start of main
/*
* rest of the code.
*
*/
/*Now I am trying to print the first 10 bytes of the main function.
* just to see if it is possible to access main's code.
*/
for(int i=0;i<10;i++)
printf("%x ",*main_ptr++);
return 0;
}
but the output is the value of the pointer (i.e. address of main function), not the value to which it points (the code of the main function). I had read somewhere that C does not dereference function pointers. But I do not know why. Is there a way to get around this?
Or, is there another way for a program to access its own code section?
P.S. I understand that many may think this is a stupid question and does not contribute significantly to research and all that. But I'am trying to understand how malware is written and given the absence of material on the web, it has been frustrating, so I decided to try this myself. Any help would be great.
There are 2 issue with your code (even though as such it is UB to be strict).
But even from any implementations point of view there are the following issues
None of the implementations define the * operator for function pointer.
None of the implementations define the ++ operator on the function pointers because size of a function is not defined.
But most implementations do define casting a fptr to void* and then to other data pointers even though it is UB.
You can make use of that fact.
I tried the simple modification of your code -
for(i=0;i<10;i++)
printf("%x ",*((int*)main_ptr++));
And it produced the "expected" behavior with gcc (MinGW64), compared the output against objdump.
Finally this goes with the warning that none of the approaches are portable. Perhaps there is no portable way to achieve what you are doing.
If you just have to get the same code as main, one way could be to read the actual binary (pointed by arg[0]). Parse the headers to find main and then read the bytes from there. Since reading the file would give you a data pointer there is no UB there.

Displaying pointer's address in C, without using printf? [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
I need to recode the printf function using only a handful of functions from the libc library, namely write, malloc and free and a few other basic ones. The only formatting tab that I am having trouble with is %p as I have no idea how to display it using the write function, as passing the address of my pointer to a function that prints strings resulted in empty output. I have also tried typecasting it to a char * and unsigned char * but that did not work either. I have looked online but only found solutions invloving forbidden functions so I am not even sure where to begin.

Can a function return a string 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 7 years ago.
Improve this question
Question is pretty much self-explainable, but I will add an example just to clarify it.
For example, we have this function:
char abc(void)
{
code...
code...
return a;
}
This function will return char a to abc, but can a function return a whole string, without using pointers, file I/O and such things?
C strings are arrays of char, terminated by a char with value 0. C functions cannot return arrays themselves, including strings, nor, indeed, can arrays be passed as arguments. Passing and / or returning pointers is C's alternative to passing and / or returning arrays. To a large extent you don't even need to do anything special to pass or return pointers in place of arrays.
Because it is somewhat cumbersome to speak precisely about these things, the pointer / array distinction is often left out of discussion and even documentation. One often refers to a char * as a string, when in fact the string is the array whose first element is pointed to. This does present the possibility of confusion, however, as is evident in the question and the associated comments.

What is the advantage of using pointers 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 8 years ago.
Improve this question
Why do we use pointers in C Programming?
In general pointers are able to access the address where the int/float/char etc... is stored.
Are there any other uses?
It depends on what you try to achieve:
you can change the value of a variable inside a function
you can pass a struct to a function without having to copy all its fields - think of a function that receives a struct.
you can point to a specific variable/struct and point to it from other structs
and many other advantages (advantages is purpose dependant and it depends on whats your program is doing).
Pointers are quite basic C and there is a lot of material online you should get yourself familiar with them and the advantages will pop up themselves.
The reason is that pointers are used to bodge into C some vital features which are missing from the original language: arrays, strings, & writeable function parameters. They can also be used to optimize a program to run faster or use less memory that it would otherwise. A few tasks these days, such as programming microcontrollers, still need this.

Is printf("%d",*(++(*a))); Undefined [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 9 years ago.
Improve this question
is printf("%d",*(++(*a))); Undefined? Here a is pointer to a pointer to integer.
I do not have anymore code other than this.It is an extension of This question which had created lots of confusions.Just want to know what is happening in this print.Does it depend on Architechture(32 Vs 64) or compiler versions.
Hoping answers will be descriptive and clear.
If you break it down, it does this:
Take the value of what a is pointing at: *a
Increment by one ++(*a)
Dereference that *(++(*a))
So, if the value+1 of what is stored at a is a valid pointer, this will work. Otherwise, the result is undefined and will most likely result in a runtime error.
Yes, your code is correct and even if cryptic can make some (little) sense as in:
void print_next(int **a) {
printf("%d\n",*(++(*a)));
}
int arr[] = {1,5,6,3,5,6};
int *p = arr;
while (p<arr+6)
print_next(&p);
If your question is specifically about *(++(*a)) expression, then there's nothing undefined here (assuming all pointers involved are valid). There are no attempts to perform multiple modifications of the same object. There are no independent reads of any of the modified objects. End of story.
Basically, there's nothing to explain here, since the code is perfectly fine in a rather straightforward manner. There's really no room for anything more "descriptive and clear" than that.
If this is not sufficiently clear, you have to explain what exactly looks suspicious to you in this expression.

Resources