1

This is my code:

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

typedef struct{
    char item[1000];
} itemset;

void additem(itemset* items,char* thing,unsigned long* numadded){
    memcpy(items->item,thing,1000);
    items++;(*numadded)++;
}

void showitems(itemset* items,unsigned long numitems){
    itemset* p=items;
    unsigned long count;
    for (count=1;count<=numitems;count++){
        printf("%d %s\n",count,p->item);p++;
    }
}

int main(){
    itemset* myitems=calloc(1,sizeof(itemset)*10);
    itemset* itemstart=myitems;
    unsigned long added=0;
    additem(myitems,"Test",&added);
    additem(myitems,"Test2",&added);
    additem(myitems,"Test3",&added);
    printf("Count=%d\n",added);
    showitems(itemstart,added);
    free(itemstart);
}

What I'm trying to see on the screen from running this code is:

Count=3
1 Test
2 Test2
3 Test3

But instead, I see:

Count=3
1 Test3
2
3

Therefore my additem function isn't working correctly. I find that a fix is to add myitems++; right after each function call, but I'm trying to make the same behavior happen inside the additem function so that I won't have to use myitems++; outside of the function. What else can I do to solve this?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Mike -- No longer here
  • 2,064
  • 1
  • 15
  • 37

4 Answers4

1

Use a double pointer for items, like this:

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

typedef struct{
    char item[1000];
} itemset;

void additem(itemset** items,char* thing,unsigned long* numadded){
    memcpy((**items)->item,thing,1000);
    (*items)++;
    (*numadded)++;
}

void showitems(itemset* items,unsigned long numitems){
    itemset* p=items;
    unsigned long count;
    for (count=1;count<=numitems;count++){
        printf("%d %s\n",count,p->item);
        p++;
    }
}

int main(void){
    itemset* myitems=calloc(1,sizeof(itemset)*10);
    itemset* itemstart=myitems;
    unsigned long added=0;
    additem(&myitems,"Test",&added);
    additem(&myitems,"Test2",&added);
    additem(&myitems,"Test3",&added);
    printf("Count=%d\n",added);
    showitems(itemstart,added);
    free(itemstart);
    return 0;
}

Output:

Count=3
1 Test
2 Test2
3 Test3

Also, check this: How to print an unsigned long int with printf in C?, which says that you should use %lu, instead of %d.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
0

I just managed to find an answer for me that works and the guy who just answered gave a similar answer too.

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

typedef struct{
    char item[1000];
} itemset;

void additem(itemset** items,char* thing,unsigned long* numadded){
    memcpy((**items).item,thing,1000);
    (*items)++;(*numadded)++;
}

void showitems(itemset* items,unsigned long numitems){
    itemset* p=items;
    unsigned long count;
    for (count=1;count<=numitems;count++){
        printf("%d %s\n",count,p->item);p++;
    }
}

int main(){
    itemset* myitems=calloc(1,sizeof(itemset)*10);
    itemset* itemstart=myitems;
    unsigned long added=0;
    additem(&myitems,"Test",&added);
    additem(&myitems,"Test2",&added);
    additem(&myitems,"Test3",&added);
    printf("Count=%d\n",added);
    showitems(itemstart,added);
    free(itemstart);
}
Mike -- No longer here
  • 2,064
  • 1
  • 15
  • 37
0

You are over writing same pointer in add item function. You would need to pass a pointer to pointer to make it work. Also use strcpy for string copy. And for showitems pass pointer allocated in calloc call as that doesn't cahnge

atamit81
  • 207
  • 1
  • 2
  • 7
-1

You need to pass items by "reference" as well, not only numadded. Or possibly return the new pointer instead.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621