Errors in this algorithm

This is a discussion on Errors in this algorithm within the C Programming forums, part of the General Programming Boards category; I have a recursive function for printed out some information from a Linked list node. Code: void generateItemReport(ItemType* currentItem) { ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    53

    Errors in this algorithm

    I have a recursive function for printed out some information from a Linked list node.

    Code:
    void generateItemReport(ItemType* currentItem) {
    
        char token[MAX_DESC_LEN + 1];
        int n, count = 0;
        char data[MAX_DESC_LEN + 1];
    
        if (currentItem != NULL) {
            generateItemReport(currentItem->nextItem);
    
            strcpy(data, currentItem->itemDescription);
    
            printf("Item ID\t: %-6s\n", currentItem->itemID);
            printf("Item Name\t: %-26s\n", currentItem->itemName);
            printf("Prices\t: $%d.%.2d $%d.%.2d $%d.%.2d\n",
                   currentItem->prices[0].dollars, currentItem->prices[0].cents,
                   currentItem->prices[1].dollars, currentItem->prices[1].cents,
                   currentItem->prices[2].dollars, currentItem->prices[2].cents);
    
            printf("Description :\n");
    
    
            while(sscanf(data, "%[^ ]%n", token, &n ) > 0) {
                count += n + 1;
                if( count < CONSOLE_MAX_LEN)
                    printf("%s ", token);
                else {
                    printf("\n");
                    count = 0;
                }
                data += n;
                if( data[0] == '\0')
                    break;
                data = data + 1;
            }
            getchar();
        }
    }
    Seems sound to me but i get an error message at two places.

    data += n; // invalid operands to binary + (have 'char[251]' and 'int')
    data = data + 1; // incompatable types when assigning to type 'char[251]' from type 'char *'

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You should never try to increment the base pointer to an array... You can crash the program's stack or cause free() to fail when using malloc().

    Code:
    char Data[MAX_DESC_LEN + 1] = {0};
    char *pData = Data;
    
    // interveining code...
    
    // increment pointer
    pData++;

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    53
    Thank you for this. Not only does it help. But it has given me an "A HA" moment.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No worries... it was a lesson learned the hard way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SVD Algorithm
    By cathy_ennis in forum C Programming
    Replies: 2
    Last Post: 12-02-2004, 02:05 PM
  2. (Im)possible Algorithm
    By Cikotic in forum C Programming
    Replies: 28
    Last Post: 03-31-2004, 08:36 AM
  3. errors.. errrors.. more errors
    By Klinerr1 in forum C++ Programming
    Replies: 17
    Last Post: 07-23-2002, 09:43 PM
  4. I need help with an algorithm
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-07-2002, 08:58 PM
  5. my grandfather's chess algorithm can beat your grandfather's chess algorithm...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 08-17-2001, 07:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21