Sasuke
i tried debugging I found that error is while freeing the temp memory.
Sasuke
Thankyou copying is getting done but am getting error one line free(temp);
Anonymous
Are there good tutorial for advanced programming c?
Modern C by Jens Gustedt TLPI by Michael Kerrisk
Hermann
👍🏿👍🏿👍🏿👍🏿
Sasuke
That's only a snippet of large code where I am swapping the two array elements which are strings so the problem is certainly not that
Sasuke
I got the things about size i was saying about string literal
Anonymous
That's only a snippet of large code where I am swapping the two array elements which are strings so the problem is certainly not that
void swap(const char *(*lhs), const char *(*rhs)) { const char *temp = *lhs; *lhs = *rhs; *rhs = temp; } try this
Anonymous
much simpler. and you can't call it on string literals directly
Sasuke
yes am using array
Sasuke
like this swapval(arr[i], arr[j]);
Sasuke
I added max function too but same error
Sasuke
arr is the array i just told you. if that am saying am giving array element as the argument why don't you believe?
Sasuke
i and j are indexes
Anonymous
You have to follow all the steps listed here. If you do that exactly, your code will work and there won't be any segmentation fault.
Anonymous
fixed a little. should work now
Sasuke
fixed a little. should work now
not working i am providing array elements as arguments no string literals in the exact code
Anonymous
swap(&arr[0], &arr[1]);
Anonymous
#include <stdio.h> void swap(const char **lhs, const char **rhs) { const char *temp = *lhs; *lhs = *rhs; *rhs = temp; } int main() { const char *arr[] = {"Hello", "World!"}; puts(arr[0]); puts(arr[1]); swap(&arr[0], &arr[1]); puts(arr[0]); puts(arr[1]); return 0; }
Sasuke
Sasuke
😊😊😊😊😊
Sasuke
But how?
Anonymous
man that just worked
remember that if the type doesn't match then this is UB. converting a char (*)[10] to const char * for example.
Anonymous
But how?
in the swap function replace const char * with int and this is an ordinary pointer based swap
Anonymous
What does UB means?
undefined behaviour
Anonymous
#include <stdio.h> void swap(const char **lhs, const char **rhs) { const char *temp = *lhs; *lhs = *rhs; *rhs = temp; } int main() { const char *arr[] = {"Hello", "World!"}; puts(arr[0]); puts(arr[1]); swap(&arr[0], &arr[1]); puts(arr[0]); puts(arr[1]); return 0; }
in this case the array has const char * members. but if you do char arr[3][4] for example, the members are of type char[4]. conversion from char (*)[4] to const char * and then accessing the data using that pointer is undefined behaviour
Sasuke
in the swap function replace const char * with int and this is an ordinary pointer based swap
i did't get the syntax you used int the function decleartion can you tell me?
Sasuke
and do this
compilation error
Anonymous
:/
Anonymous
void swap(int *lhs, int *rhs) { int temp = *lhs; *lhs = *rhs; *rhs = temp; }
Anonymous
i simply replaced int with const char * here
Sasuke
😱 man I am getting crazy
Anonymous
😱 man I am getting crazy
oof. in that case you need to read a bit about pointers
Sasuke
Initially it was char * and I passed argument arr[0] which is also a char *
Sasuke
But when you make it const char ** and I passed &arr[0] which is address of char *
Anonymous
Initially it was char * and I passed argument arr[0] which is also a char *
ye. and you tried to replace the char contents pointed to by the char *
Anonymous
But when you make it const char ** and I passed &arr[0] which is address of char *
i did not replace the contents of the string, i replaced the contents of your array
Anonymous
his examples were string literals though 🤷‍♂️
Anonymous
But content of the array are characters
my code works if you have two variables where you have the addresses to two string literals stored.
Sasuke
Isn't this syntax tells you I and j are indexes of same arr array
Sasuke
Array of strings
Anonymous
Array of strings
ye this was my assumption too
Sasuke
You are asking for the exact coding from the begining you didn't ask me what type of array it is
Sasuke
But anyways thanks both of you
Anonymous
she did ask what was arr
Sasuke
You asked me? Go back and show me the message
Sasuke
You didn't even asked me which type of array it is ? Show me when you said which type of array. And you are free to go
MᏫᎻᎯᎷᎷᎬᎠ
😂😂😂
Anonymous
like look at my answers. every time i mentioned an array i either mentioned its type or the member type @Devil420H
Sasuke
the question is implicit in what is arr
You said I have to read about pointers from where I can read?
Anonymous
From where?
Prata. just understanding my int example should be enough.
Sasuke
Prata. just understanding my int example should be enough.
And not getting your example that's why am asking
Anonymous
Prata. just understanding my int example should be enough.
then you can replace any type for int and do the same thing
Sasuke
then you can replace any type for int and do the same thing
But am not getting how it's happening
Anonymous
But am not getting how it's happening
;-; Prata = C Primer Plus by Stephen Prata
Anonymous
typically people who post puzzles have them as assignments or something. they expect users to do it for them and then copy paste the code. you are probably going to give a puzzle for the sake of the puzzle. so i don't see why not.
Anonymous
/report
Anonymous
btw how does this tag work? is it different from @ tagging?
Anonymous
ye