Skip to content

Conversation

@MolDuM
Copy link
Contributor

@MolDuM MolDuM commented Dec 21, 2022

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.

  1. malloc is not used to define an array in C++. But to free the space of the temporary array, I add an initializing way, but it is not smart.
  2. Since malloc cannot initialize the array, I initialized it by myself. I am wondering if calloc should be introduced to reader in this part.

I hope the reviewer could give some advice.

Tip: If this PR is not related to the coding or code translation, please ignore the checklist.

Checklist

  • I've tested the code and ensured the outputs are the same as the outputs of reference codes.
  • I've checked the codes (formatting, comments, indentation, file header, etc) carefully.
  • The code does not rely on a particular environment or IDE and can be executed on a standard system (Win, macOS, Ubuntu).

@vercel
Copy link

vercel bot commented Dec 21, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Updated
hello-algo ⬜️ Ignored (Inspect) Jan 4, 2023 at 5:11PM (UTC)

/* 扩展数组长度 */
int* extend(int* nums, int size, int enlarge) {
// 初始化一个扩展长度后的数组
int* res = (int *)malloc(sizeof(int) * (size + enlarge));
Copy link
Owner

@krahets krahets Dec 22, 2022

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;

Copy link
Contributor Author

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.

Copy link
Owner

@krahets krahets Dec 22, 2022

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. 🤣

Copy link
Owner

@krahets krahets left a 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.

@krahets krahets changed the title create c folder and add array code Add array code in C Dec 22, 2022
@krahets krahets added the code Code-related label Jan 2, 2023
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.
@MolDuM
Copy link
Contributor Author

MolDuM commented Jan 4, 2023

I have just added a commit. I finally chose a friendly way for beginner instead of the cleverest way.

reference:
[1] https://en.cppreference.com/w/c
[2] 《C程序设计》谭浩强

Copy link
Owner

@krahets krahets left a 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!

@krahets krahets merged commit 2c21f54 into krahets:master Jan 4, 2023
@Gonglja Gonglja mentioned this pull request Mar 18, 2023
51 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code Code-related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants