I am using Mac OS to implement part of function of command sort:
part of my code is below:
int compare(const void *p, const void *q) {
return strcmp(*(char **)p, *(char **)q);
}
void sort_file(char *filename, int unique, int reverse) {
/* TODO: Complete this function */
/* Note: you will probably need to implement some other functions */
char buf[1024][1024];
int i=0;
FILE * fp;
if(!(fp=fopen(filename,"r"))){
perror("Open error!");
exit(0);
}
while(fgets(buf[i],1024,fp)){
printf("%s",buf[i]);
i++;
}
qsort(buf, i, sizeof(char *), compare);
}
The result always show segmentation fault: 11
Anyone can tell me what's the problem and how to modify it?
I still want to know, if I don't know the maximum size of code in one line and in the file, how to define my array?
I get the idea from this page:
http://www.anyexample.com/programming/c/qsort__sorting_array_of_strings__integers_and_structs.xml
You don't have an array of pointers; you have an array of arrays. Each element in your array of arrays has a specific, fixed stride of 1024 char. qsort needs to know that, and you're not telling it. First change this:
qsort(buf, i, sizeof(char *), compare);
to this:
qsort(buf, i, sizeof *buf, compare);
Now qsort knows how big each "thing" is in your array of char arrays.
Next, your comparator should be altered to account for the address being passed and what it is as it pertains to your array of arrays. Each address passed to the comparator is where an element lays. But your elements are each char[1024]. The address of some char[1024] isn't some char**, it is char(*)[1024]. There are no pointers to pointers involved here. Your comparator can simply be:
int compare(const void *p, const void *q)
{
const char (*lhs)[1024] = p;
const char (*rhs)[1024] = q;
return strcmp(*lhs, *rhs);
}
Next, there is no limiter in you control loop to prevent overflowing your array of arrays. In short, this:
while(fgets(buf[i],1024,fp))
should be this:
while(i < 1024 && fgets(buf[i],1024,fp))
and ideally, that 1024 should be expressed as a constant somewhere to avoid magic number sprinkling.
Finally, you're leaking an open FILE* in your function. Not a good plan. Make sure to fclose() your file pointer.
Problems I see:
Wrong argument to qsort
qsort(buf, i, sizeof(char *), compare);
Needs to be:
qsort(buf, i, sizeof(buf[0]), compare);
Since the difference between buf+1 and buf is 1000 chars, it is wrong to use sizeof(char*) as the third argument.
Wrong casting of the arguments in compare
The original pointers are of type char (*)[1000], not char**. Hence you need to use:
int compare(const void *p, const void *q) {
char (*p1)[1000] = (char (*)[1000])(p);
char (*q1)[1000] = (char (*)[1000])(q);
return strcmp(*p1, *q1);
}
Related
I'm trying to figure out how to "transform" strings (char*) to void* and viceversa.
When I execute this my output is just the first printf and ignores the second one, it doesn't even write "after = "
PS This little program is just to understand, I know i could actually use swap(&s[0],&s[1]). I need to know how to properly cast a void pointer into an array of strings.
I'm working on a uni project where I need to create my own quick_sort algorythm and I need the swap function inside of it to work with void pointers.
#include <stdio.h>
#include <stdlib.h>
static void swap(char** x,char** y);
static void swap(char** x,char** y){
char* temp=*x;
*x=*y;
*y=temp;
}
int main()
{
char* s[2];
s[0]="weee";
s[1]="yooo";
void* array=s;
printf("before %s %s\n",s[0],s[1]);
swap((&array)[0],(&array)[1]);
printf("after = %s %s",(char*)array,(char*)array);
return 0;
}
I think I'm missing something big
Thanks in advance :D
In this declaration the array s used as an initializer is implicitly converted to a pointer to its first element of the type char **.
void* array = s;
In the call of the function swap
swap((&array)[0],(&array)[1]);
the first argument can be the pointer array itself that will be implicitly casted to the pointer type of the corresponding parameter
swap( array, (&array)[1]);
But you need to correctly pass the second argument. To do this you need to cast the pointer array explicitly like
swap( array, ( char ** )array + 1 );
In the call of printf you need also correctly to supply argument expressions.
Here is your updated program
#include <stdio.h>
static void swap(char** x,char** y);
static void swap(char** x,char** y){
char* temp=*x;
*x=*y;
*y=temp;
}
int main()
{
char* s[2];
s[0]="weee";
s[1]="yooo";
void* array=s;
printf("before %s %s\n",s[0],s[1]);
swap( array, ( char ** )array + 1 );
printf("after = %s %s", *(char**)array, ( (char**)array )[1]);
return 0;
}
The program output is
before weee yooo
after = yooo weee
void *array = s; declares array to be a void *. Then &array is the address of that void *, so &array[1] would access a void * after it. But there is no void * after it, since void *array defines a single void *.
array could be properly defined to alias s with char **array = s;, after which swap(&array[0], &array[1]); would work as desired.
If you define array as void **array = (void **) s;, then swap(&array[0], &array[1]); will produce diagnostic messages because the types are wrong. You could use swap((char **) &array[0], (char **) &array[1]);.
Then, if you print the strings with printf("after = %s %s", array[0], array[1]);, this will work, although it is not entirely proper code. Using array[0] as an argument passes a void * where printf is expecting a char * for the %s. However, the C standard guarantees that void * and char * have the same representation (encode their values using bytes in memory in the same way), and it further says (in a non-normative note) that this is intended to imply interchangeability as arguments to functions.
The void* doesn't seem to fulfil any particular purpose here, just swap the pointers: swap(&s[0],&s[1]);.
You could also do this:
char** ptr = &s[0];
printf("before %s %s\n",ptr[0],ptr[1]);
swap(&ptr[0],&ptr[1]);
printf("after = %s %s",ptr[0],ptr[1]);
If you for reasons unknown insist on using void* then note that as your code stands, it points at the first char* in your array of char*. However, it isn't possible to perform pointer arithmetic on void* since that would entail knowing how large a "void" is. The void* doesn't know that it points at an array of pointers. Therefore array[i] is nonsense.
Also, the void* are set to point at char* so you simply cannot pass it to a function expecting a char**. You'd have to rewrite the whole program in a needlessly obfuscated way, so just abandon that idea.
I am slamming my head against the wall with this problem.
To summarize:
I need to dynamically add strings to an array, sort them, and then check against another string value.
This needs to work on a SCADA-system that support C as a scripting language, but with limited functionality. I have qsort() available.
However, with the test code I have, I am not able to use qsort on an array, with values that are added dynamically.
To be clear, I can add strings to the array, which works fine.
However when I call qsort() on that array, I can no longer print out the indices.
Heres is the code so far (be kind, I'm not very proficient in C):
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int cstring_cmp (const void *a, const void *b)
{
// This function is taken from an online example
const char **ia = (const char **) a;
const char **ib = (const char **) b;
return strcmp (*ia, *ib);
}
int main ()
{
//char *ArchiveKomponents[] = {"R1890L", "F1121D", "F1284Z", "A1238K"};
// If I do the above commented out, it works as intended
char ArchiveKomponents[100][20];
strcpy(ArchiveKomponents[0], "R1890L");
strcpy(ArchiveKomponents[1], "F1284Z");
size_t strLen = sizeof (ArchiveKomponents) / sizeof (char *);
printf ("Len: %zu\n", strLen);
printf ("Before [0]: %s\n", ArchiveKomponents[0]);
printf ("Before [1]: %s\n", ArchiveKomponents[1]);
qsort (ArchiveKomponents, (size_t)strLen, sizeof (char *), cstring_cmp);
printf ("After [0]: %s\n", ArchiveKomponents[0]);
printf ("After [1]: %s\n", ArchiveKomponents[1]);
// When run, the "After" prints are not even printed, the program simply halts
return 0;
}
I feel that I have googled the entire internet, in search of an answer on how to do this, with no luck.
Regards
You are comparing incorrect types. The comparison functions treats 4 or 8 characters from the element as a pointer to a string. Dereferencing this pointer triggers Undefined Behavior, likely a crash.
Note, that the type of a single element is char[20] not char*. Therefore your comparison function could be simply implemented as:
int cstring_cmp (const void *a, const void *b)
{
return strcmp (a, b);
}
Pointers a and b points to arrays of 20 character. The address of array is the same as an address of its first element. So a and b can be used as pointers to chains of char (aka "c-strings").
Moreover, void* is automatically converted to any pointer type without casting.
The qsort invocation should be:
qsort (ArchiveKomponents, // array to be sorted
2, // number of elements in the array
sizeof ArchiveKomponents[0], // size of a single element
cstring_cmp // comparison function
);
I have a list of character arrays malloced as follows.
list= (char**)malloc(LIST_SIZE* sizeof(*list));
for (int i = 0; i < LIST_SIZE; i++)
list[i] = (char*)malloc(sizeof(char) * 33);//33 is the size of each element in the list
The size of the list can grow as big as 25,000(LIST_SIZE) or sometimes may be just 10 elements or less.
I have the compare() function which is giving errors.
int compare(const void * a, const void * b)
{
const char **fpa = (const char**)a;
const char **fpb = (const char**)b;
return strcmp(*fpa, *fpb);
//return *(char *)a - *(char*)b;//This one also does not work
}
qsort() is invoked like this. I suspect it fails because of the way it is invoked but I could not understand why.
qsort(list, current_list_element_count, 33, compare);
You're passing in the wrong value for the element size to qsort.
Even though you allocate 33 bytes for each array, you don't actually have an array of arrays. What you have is an array of pointers, each of which points to an array.
Since each member of the array is a char *, you want to pass the size of that:
qsort(list, current_list_element_count, sizeof(char *), compare);
Or more generally:
qsort(list, current_list_element_count, sizeof(*list), compare);
This way, what you pass for the size doesn't depend on the type.
I'm beginning to learn C and read following code:
public void** list_to_array(List* thiz){
int size = list_size(thiz);
void **array = malloc2(sizeof(void *) * size);
int i=0;
list_rewind(thiz);
for(i=0; i<size; i++){
array[i] = list_next(thiz);
}
list_rewind(thiz);
return array;
}
I don't understand the meaning of void**. Could someone explain it with some examples?
void** is a pointer to a pointer to void (unspecified type). It means that the variable (memory location) contains an address to a memory location, that contains an address to another memory location, and what is stored there is not specified. In this question's case it is a pointer to an array of void* pointers.
Sidenote: A void pointer can't be dereferenced, but a void** can.
void *a[100];
void **aa = a;
By doing this one should be able to do e.g. aa[17] to get at the 18th element of the array a.
To understand such declarations you can use this tool and might as well check a related question or two.
void** is a pointer to void*, or a pointer to a void pointer if you prefer!
This notation is traditionally used in C to implement a matrix, for example. So, in the matrix case, that would be a pointer to an array of pointers.
Normally void * pointers are used to denote a pointer to an unknown data type. In this case your function returns an array of such pointers thus the double star.
In C, a pointer is often used to reference an array. Eg the following assignment is perfectly legal:
char str1[10];
char *str2 = str1;
Now when void is used, it means that instead of char you have a variable of unknown type.
Pointers to an unknown data type are useful for writing generic algorithms. Eg. the qsort function in standard C library is defined as:
void qsort ( void * base,
size_t num,
size_t size,
int ( * comparator )
( const void *, const void * ) );
The sorting algorithm itself is generic, but has no knowledge of the contents of the data. Thus the user has to provide an implementation of a comparator that can deal with it. The algorithm will call the comparator with two pointers to the elements to be compared. These pointers are of void * type, because there is now information about the type of data being sorted.
Take a look at this thread for more examples
http://forums.fedoraforum.org/showthread.php?t=138213
void pointers are used to hold address of any data type. void** means pointer to void pointer. Void pointers are used in a place where we want a function should receive different types of data as function argument. Please check the below example
void func_for_int(void *int_arg)
{
int *ptr = (int *)int_arg;
//some code
}
void func_for_char(void *char_arg)
{
char *ptr = (char *)char_arg;
//some code
}
int common_func(void * arg, void (*func)(void *arg))
{
func(arg);
}
int main()
{
int a = 10;
char b = 5;
common_func((void *)&a, func_for_int);
common_func((void *)&b, func_for_char);
return 0;
}
I am trying to write a program that will mutliply two numbers, but output the result in binary, showing the calculation (i.e. shifting the rows). I'm storing the binary numbers as an array of 32 characters, and each element is either 1 or 0.
I have a function that will convert decimal to binary character array, but then my array only exists within the function, but I need to use the array within another function and also within main. I was thinking it might be possible to use pointers to change the value of the array in main from within my converter function, so then the array will exist in main and can be used in other functions. Is this possible?
I've declared two pointers to character arrays:
char (*binOne)[32];
char (*binTwo)[32];
if I pass the pointer as a parameter to the function, can I still access each element? Sorry if I'm not making much sense here.
In C, most of the time array behaves like pointer to its first element, so what you probably want to do is:
void foo(char* arr){
//some stuff, for example change 21-th element of array to be x
arr[20] = 'x';
}
int main(){
char binOne[32];
char binTwo[32];
// change array binOne
binOne[20] = 'a';
foo(binOne);
// now binOne[20] = 'x'
foo(binTwo);
// now binTwo[20] = 'x'
}
A continuation of what I added as a comment:
In C, if you want to modify/return an array, you'll do that by passing a pointer to it as an argument. For example:
int toBinary(char *buff, int num) { /* convert the int, return 1 on success */ }
...
char buff[32];
toBinary(buff, 9001);
In C, an array's name is it's address, it's the address of the first element:
buff == &buff == &buff[0]
Yes, this is possible. But you only need a pointer to the array not an array of pointers.
You need to prototype like
e.g.
void int_to_arr(int n, char *arr);
void arr_to_int(int *n, char *arr);
in main(){
char *binarr = calloc(32, sizeof(char));
int n = 42;
int_to_arr(n, binarr);
}
void int_to_arr(int n, char *arr)
{
//do you conversion
//access array with
// arr[i]
}
void arr_to_int(int *n, char *arr)
{
//do you conversion
//access array with
// *n = result;
}