0

in order to complete a larger project, im trying to get an idea of how to send an array of structures, and a token of char* type to a function. my Pupose of this code is to do the following:

  • open file
  • tokenize file
  • send token,and array of structures to search function
  • search function will go through the arrayofstructures, using strcmp to find a match with token
    • if match found return 1, the main function will check for 1 or 0
    • if 0, dont add token to array of structures,if 1 add token to arrayof structures

i just wrote a small program to see if i could send the array,and token to a function and compare but i get so many errors im lost at what to do since i dont understand most of the errors.

#include <stdio.h>
#include <string.h>

int search(struct id array[],char* tok);

struct id
{
    char name[20];
    int age;
};

int main(void)
{
    struct id person[2] = { {"John Smith", 25},
                            {"Mary Jones", 32} };
    char* token = "Mary Jones"; /*char* for strtok() return type*/
    search(person,token);
}

int search(struct id array[],char* tok)
{
    int i,value;int size = 2;
    for(i=0;i<size;i++)
    {
        if(strcmp(array[i].name,tok) == 0)
            value = 0;
        else
            value = 1;
    }
    return value;
}
2
  • 2
    So, to paraphrase the first paragraph, you don't have any idea of arrays and pointers, but still have a large project just needing its final touch? curious. Commented Jul 8, 2014 at 20:13
  • 1
    not its final touches,im building up to it.i have an idea of pointers and structures but when it comes to sending stuff to functions i always mess it up. Commented Jul 8, 2014 at 20:21

3 Answers 3

2

Place

int search(struct id array[],char* tok);  

after struct declaration. And assign the return value from search to an int variable.

int found = search(person,token);  
if(found == 0)
    printf("Name is found\n"); // or whatever you want
Sign up to request clarification or add additional context in comments.

Comments

0

Here is the code that you should use.

#include <stdio.h>
#include <string.h>

struct id
{
    char name[20];
    int age;
};

int search( const struct id array[], size_t n, const char *tok );

int main( void )
{
    struct id person[2] = { {"John Smith", 25},
                            {"Mary Jones", 32} };
    char* token = "Mary Jones"; /*char* for strtok() return type*/

    printf( "%d\n", search( person, sizeof( person ) / sizeof( *person ), token ) );

    return 0;
}


int search( const struct id array[], size_t n, const char *tok )
{
    size_t i = 0;

    while ( i < n && strcmp( array[i].name, tok ) != 0 ) ++i;

    return n != 0 && i != n;
}

EDIT: I removed some typos.

The output is

1

that is the name has been found.

Take into account that the correct function search has to return 1 if the name is found.:)

2 Comments

what will sizeof(person)/sizeof(*person) return? the total number of elements in the array which would be 2?
@user368 sizeof( person ) returns the number of bytes that were allocated for the array. sizeof( *person ) return the number of bytes in one element of the array (that is of the first element). So dividing the first by the second you will get the number of elements in the array.
0

Always declare structs / enums / unions before defining pointers to them, or using them in a function declaration, like this:

struct id;

Extra tip, introduce a typedef-name with the same id as the tag name at the same time:

typedef struct id id;

Always declare functions before first use, like this:

int search(struct id array[],char* tok);

Always define structs / enums / unions before using them for anything but what the first rule covers, like this:

struct id {
    char name[20];
    int age;
};

With typedef-name:

typedef struct id { /**/ } id;

Now, where possible, put the definition where you would otherwise need to put a forward-declaration.
Only exception: If you put the declaration in a header-file, that's fine.
That reduces superfluous redundancy.


Some more observations:

  • Don't use fixed-size fields for names and such. They are always too short.
  • Never modify string literals, failing to heed that prohibition results in Undefined Behavior, your program just became meaningless. Work with a copy instead.

    /* Most implementations supply this non-standard function */
    char* strdup(const char* s) {
      size_t n = strlen(s)+1;
      char* p = malloc(n);
      if(p) memcpy(p, s, n);
      return p;
    }
    
  • When you pass an array, you are actually only passing a pointer to its first element, so pass an element-count too.
    • Types size_t or ssize_t are designed for that chore.
    • If you have an array named a, you get the element count using sizeof a / sizeof *a. Be sure that's not a pointer though!
  • Early return are good: Return success as early as possible.
    Then you don't chance to forget your success in the next loop iteration (as happened to you), beside being faster.

1 Comment

Fixed size fields are fine, as long as you make sure to not overflow them. (Which is less drama than making sure to insert special code every time you create, copy, or destroy an object).

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.