#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    Cannot pass and return a character array


    read-file-inter0.c: In function `read_file_nic':
    read-file-inter0.c:22: subscripted value is neither array nor pointer
    read-file-inter0.c:23: subscripted value is neither array nor pointer
    read-file-inter0.c:27: subscripted value is neither array nor pointer
    read-file-inter0.c:29: subscripted value is neither array nor pointer
    read-file-inter0.c:30: subscripted value is neither array nor pointer
    read-file-inter0.c:33: warning: passing arg 1 of `strcmp' makes pointer from integer without a cast
    read-file-inter0.c: In function `main':
    read-file-inter0.c:50: incompatible types in assignment
    read-file-inter0.c:51: incompatible types in assignment

    /**********************************/
    1 #include <stdio.h>
    2 #include <stdlib.h>
    3 #include <string.h>
    4 #include <sys/types.h>
    5 #include <sys/stat.h>
    6 #include <unistd.h>
    7
    8 char read_file_nic(file_name, word)
    9
    10 {
    11 FILE *inter0, *inter1, *fopen();
    12 int i = 0;
    13 char single_char;
    14
    15 int result = 99;
    16
    17 inter0 = fopen(file_name, "r");
    18
    19 single_char = getc(inter0); /* read first character */
    20 while(!feof(inter0) && single_char != '\n')
    21 {
    22 word[i] = single_char;
    23 printf ("i = %d %c\n", i, word[i]);
    24 i++;
    25 single_char = getc(inter0);
    26 }
    27 word[i] = '\0'; /* terminate that word. replace the '\n' with '\0' */
    28
    29 printf("word[17] = %c\n", word[17]);
    30 printf("word[18] = %c\n", word[18]);

    printf("word = %s\n", word);
    33 result = strcmp (word, "00:E0:18:06:1C:64");
    printf("result = %d\n", result);
    printf("\n");
    return(word);
    }

    /*************************/

    int main()

    {

    char word[17] = "12345678901234567";
    char word0[17];
    char word1[17];
    char file_name0[] = "/var/apache/inter0";
    char file_name1[] = "/var/apache/inter1";

    50 word0 = read_file_nic(file_name0, word);
    51 word1 = read_file_nic(file_name1, word);

    printf("word0 = %c\n", word0);
    printf("word1 = %c\n", word1);
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    19
    Your read_file_nic function doesn't specify the parameter types so the compiler is assuming that they are of type int.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    Thumbs up Problem solved


    Don 't try to solve this one, since I have already figure it out. Save your valuable resource for someone else.

    ==========================================
    Error 22, 23, 27, 29 and 30 occurred because
    char word[17] was not declared in the in the
    read_file_nic (file_name, word) function.

    In the read_file_nic function, the return statement should be
    return(word[i]), not return(word);

    Error # 33, 50 and 51 are the result of of assigning a string to a string as in a = b. In C this is illegal. to assign a string array to another string array use the strncpy(s1, s2, n) function.


    =============================================
    Here is the solution. You can compare this code with the previous one

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>

    char read_file_nic(file_name, word)
    char word[17];
    {
    FILE *inter0, *inter1, *fopen();
    int i = 0;
    char single_char;
    /* char word[17]; */
    int result = 99;

    inter0 = fopen(file_name, "r");

    single_char = getc(inter0); /* read first character */
    while(!feof(inter0) && single_char != '\n')
    {
    word[i] = single_char;
    printf ("i = %d %c\n", i, word[i]);
    i++;
    single_char = getc(inter0);
    }
    word[i] = '\0'; /* terminate that word. replace the '\n' with '\0' */

    printf("word[17] = %c\n", word[17]);
    printf("word[18] = %c\n", word[18]);

    printf("word = %s\n", word);
    result = strcmp (word, "00:E0:18:06:1C:64");
    printf("result = %d\n", result);
    printf("\n");
    return(word[i]);
    }

    int main()

    {

    char word0[17];
    char word1[17];
    char file_name0[] = "/var/apache/inter0";
    char file_name1[] = "/var/apache/inter1";

    read_file_nic(file_name0, word0);
    read_file_nic(file_name1, word1);

    printf("word0 = %s\n", word0);
    printf("word1 = %s\n", word1);
    }
    Last edited by linh; June 13th, 2003 at 09:47 AM.
  6. #4
  7. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    In the future, if you have solved a problem, it would help us and others if you posted either that it was solved or what you did to solve it.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    Smile which button do I click to indicate that the problem is solved


    1) which button do I click to indicate that I have solved my own problem that was posted ?

    2) Which button do I click to rate a someone who have helped me ?
  10. #6
  11. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    There isn't a button.
    You can either a) post in the thread again that the problem was solved. or b) request that the thread by closed by a moderator (me) or c) delete the thread by deleting your first post.

    As far as I know there isn't a rating for members.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    South Africa
    Posts
    3
    Rep Power
    0

    Passing Arrays


    Cool, I guees you think you have really solved that one for good, try this one for size.

    After a while runnignthis program you will run into a possible crash due to some FFFFFFsomething error. You will not be able to explain it, after all you did everythign right.

    Check this our, never pass arrays to a functiontion that does not want them, rather overload the function and use objects instead. This will go a long way into simplifying your programming life.
  14. #8
  15. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    As far as I know, overloading and objects both are not part of C, they are a part of C++.

IMN logo majestic logo threadwatch logo seochat tools logo