-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Add array code in C #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add array code in C #154
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
| /* 扩展数组长度 */ | ||
| int* extend(int* nums, int size, int enlarge) { | ||
| // 初始化一个扩展长度后的数组 | ||
| int* res = (int *)malloc(sizeof(int) * (size + enlarge)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I'm not familiar with C. But how about initializing the array pointer in this way:
int arr[size + enlarge];
int *res = arr;There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is not a good idea in C.
The first line defines a local variable array in the function, which means the address would be assigned to arr, and it would be freed when return. In another word, the numbers in arr would be lost after the function finished. Of course we can define a new array outside the function, but it would reduce the transportability.
Besides, the first line is not recommended in C because the length of array should be a constant value instead of a variable. There would be a reminder from IDE (but no warning and error) on my VSCode.
This is a common problem in C. In my experience, I would use malloc to define an unknown length array and use realloc to change the length. But it is not the classical way to define an array. I think I need to have a look at the official document and update it later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good explanation. 👍
Actually, I think the array in C/C++ is too complex to use. In practice, We'd like to use vector as an array in C++ in 100% conditions. However, this project is a book. 🤣
krahets
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank @MolDuM for the C codes! You are the first contributor writing in C. Please address the comments.
3 changes: 1. In the enlarge part, I didn't change because I didn't find a good way to deal with the local variable and the clear definition. malloc is commonly used in LeetCode so I think it is not bad for a beginner. 2. I changed the initialization of the second array to make it in the same style as C++. 3. In the enlarge part in main, I deleted the code of pointer free to match the array operations. I also changed the operate array in the later part because the enlarged array cannot be assigned to the older array name. BTW, sorry for updating so late. Reading different version documents and book are really tiring and boring.
|
I have just added a commit. I finally chose a friendly way for beginner instead of the cleverest way. reference: |
krahets
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the great work!
My C code is mainly referred to C++ code since the similar grammar. The output of Java is also concerned.
There are two questions on the definition of array.
I hope the reviewer could give some advice.
Checklist