5

I have to write a function that finds a product with given code from the given array. If product is found, a pointer to the corresponding array element is returned.

My main problem is that the given code should first be truncated to seven characters and only after that compared with array elements.

Would greatly appreciate your help.

struct product *find_product(struct product_array *pa, const char *code) 

{   
char *temp;
int i = 0;
    while (*code) {
        temp[i] = (*code);
        code++;
        i++;
        if (i == 7)
            break;
    }
    temp[i] = '\0';

for (int j = 0; j < pa->count; j++)
    if (pa->arr[j].code == temp[i])
        return &(pa->arr[j]);
}
4
  • 5
    First thing I noticed was temp is not initialized and you are using it which will lead to undefined behavior Commented Apr 9, 2015 at 7:38
  • 2
    You did not allocate any memory for temp. Use malloc/calloc to do it. And what did you mean by "given code should first be truncated to seven characters"? Commented Apr 9, 2015 at 7:39
  • 1
    if (pa->arr[j].code == temp[i]) is the same as if (pa->arr[j].code == 0). Use strcmp or strncmp to compare two strings Commented Apr 9, 2015 at 7:40
  • 1
    You can use strncpy() to copy the first 7 characters of code to temp, you don't need to write your own loop. Commented Apr 9, 2015 at 7:41

4 Answers 4

10

Why don't you just use strncmp in a loop?

struct product *find_product(struct product_array *pa, const char *code) 
{ 
   for (size_t i = 0; i < pa->count; ++i)
   {
      if (strncmp(pa->arr[i].code, code, 7) == 0)
          return &pa->arr[i];
   }
   return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

4

temp is a pointer which is uninitialized and you are dereferencing it which will lead to undefined behavior.

temp = malloc(size); // Allocate some memory size = 8 in your case

One more mistake I see is

if (pa->arr[j].code == temp[i]) // i is already indexing `\0` 

should be

strcmp(pa->arr[j].code,temp); // returns 0 if both the strings are same

This code can completely be avoided if you can use strncmp()

Comments

2

As pointed out by others, you are using temp uninitialized and you are always comparing characters with '\0'.

You don't need a temp variable:

int strncmp ( const char * str1, const char * str2, size_t num );

Compare characters of two strings

Compares up to num characters of the C string str1 to those of the C string str2.

/* Don't use magic numbers like 7 in the body of function */
#define PRODUCT_CODE_LEN 7

struct product *find_product(struct product_array *pa, const char *code) 
{   
    for (int i = 0; i < pa->count; i++) {
        if (strncmp(pa->arr[i].code, code, PRODUCT_CODE_LEN) == 0)
            return &(pa->arr[i]);
    }
    return NULL; /* Not found */
}

1 Comment

UB galore if the product is not to be found.
1

When you write char* temp; you are just declaring an uninitialized pointer

In your case since you say that the code is truncated to 7 you could create a buffer on the stack with place for the code

char temp[8];

Writing

temp[i] = (*code);
code++;
i++;

Can be simplified to:

temp[i++] = *code++;

In your loop

for (int j = 0; j < pa->count; j++)
    if (pa->arr[j].code == temp[i])
        return &(pa->arr[j]);

You are comparing the address of code and the character value of temp[i] which incidentally could be 8 and outside the array.

Instead what you want to do is compare what code points to and what temp contains:

for (int j = 0; j < pa->count; j++)
    if (!strncmp(pa->arr[j].code, temp, 7)
        return &(pa->arr[j]);

You should also return NULL; if nothing was found, seems you do not return anything.

Probably a good thing is also to make sure your temp[] always contains 7 characters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.