Skip to content
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

initial commit #15

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

sashat14
Copy link

No description provided.


free(arr->elements);
free(arr);
arr = NULL;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious as to why you have this line here. What purpose does it serve?

@@ -99,15 +99,25 @@ char *arr_read(Array *arr, int index) {
void arr_insert(Array *arr, char *element, int index) {

// Throw an error if the index is greater than the current count

if(index >= arr->count){
printf("Array capacity has been reached");
Copy link

@adrianadames adrianadames Jan 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error message should say that the index is out of range.

One possible change you might want to make to your arrays program is using stderr (standard error stream) and fprintf() (for writing data to a stream) to record and print error messages like so: fprintf(stderr, "IndexError: Index out of range").

See: https://stackoverflow.com/questions/19870331/why-use-stderr-when-printf-works-fine.

@@ -85,7 +85,7 @@ char *arr_read(Array *arr, int index) {

// Throw an error if the index is greater than the current count
if(index >= arr->count){
printf("Capacity has been reached");
printf("Array capacity has been reached");
return 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might throw an error because this function is set up to output void (i.e. doesn't output anything). Here you're outputting an integer.

// Move every element after the insert index to the right one position

for(int i = index +1; i<arr->count; i++){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part here needs to be corrected. Try using python tutor to see exactly what is happening during this step of the arr_insert function.

// Copy the element and add it to the array
char *element_cpy = element;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it is now, the element_cpy pointer will point to the string pointed to by the input element pointer. What you want is to malloc a chunk of memory, write the string to the chunk of memory, and have the element_cpy pointer point to this newly copied string. The strdup() function does this for us. Brady went into this a little bit during lecture.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants