Thread: char **

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    22

    char **

    I'm working with code that grabs strings from a file and then works with said strings in different ways.

    I would like to check if the first character in a char* is a #:

    Code:
    FILE* file = file_open(filename);
    char* fileline = malloc(160);
    file_getline(fileline,file);
    printf("Test: %s\n", &fileline[0]);
    Where the two methods are:

    Code:
    /***************
     * These functions are just some handy file functions.
     * We have not yet covered opening and reading from files in C,
     * so we're saving you the pain of dealing with it, for now.
     *******/
    FILE* file_open(char* filename) {
            FILE* fp = fopen(filename, "r");
            if(fp == NULL) {
                    fprintf(stderr, "Error opening file %s, abort.\n", filename);
                    exit(1);
            }
            return fp;
    }
    
    /*******************
     * To use this function properly, create a char* and malloc
     * 160 bytes for it. Then pass that char* in as the argument
     ******************/
    char* file_getline(char* buffer, FILE* fp) {
            buffer = fgets(buffer, 160, fp);
            return buffer;
    }
    But when I reference the text with &fileline[0], it prints out the entire string.

    Moreover, why is this string suddenly a char** when I attempt to print it? If i just do printf("Test: %s\n", filename) the code works, but if i try printf("Test: %s\n", filename[0]) it yells at me.

    What am I missing here?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    char *fileline = malloc(160)
    This is a pointer to a char. It points to a contiguous block of 160 chars, that can be treated as an array through indexing, like so: filename[i].

    &filename[0] is the address of the first (zeroth) element in the array, which is synonymous with filename.

    filename[0] is the first element in the array, which is just a plain char. %s requires a char * pointing to the beginning of a null-terminated array of chars. Use %c for printing an individual character like fileline[0].

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    That's because fileline[0] is only a char. The %s format specifier tells printf() to print a string. If you want to just print the first character of a string, then use %c:
    Code:
    printf("%c\n", fileline[0]);
    Or...
    Code:
    if(fileline[0] == '#')  // Equivalent to if(*fileline == '#')
      // Do something
    Likewise with the filename. filename[0] is a char, not a char *, so you should be using %c to print it instead of %s.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    22
    Thanks!

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It's a bit of C screwyness...

    filename.... is the address of the array holding the string.

    filename[10] is the 11th character in the string and returns only 1 character by value.

    Essentially the [] notation dereferences the address + offset for you.

    Sooo...
    printf("%s", filename); is correct.
    printf("%c",filename[1]); is correct.
    printf("%s", &filename[1]); is correct.

    I know it's confusing... I trip over it all the time...

    For example... testing for # as the first character:
    Code:
    if (filename[0] == '#')
      DoMyFancyThingHere();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM