1

I have a typedef structnamed "item" that contains 2 char[254] (Name of product and Name of company) and 9 int variables. (Price, Amount,etc..).

I created a pointer from that typedef struct and one array(1D) and one Two Dimensional array.

I have used scanf to store data to the pointer's respective variables (No problem so far).

Now, I want to "copy and store" the data of the pointer's variables into the array (1D) then "copy and store" the data of the 1D array to the 2D array.

For pointer to 1D array, this is what I did:

void pointer_conversion(item *a, item curr[10000], int total)
{
memcpy(&curr[total], a, sizeof(item*));
} 
// Tried doing: memcpy(&curr[total],a,sizeof(item*) * 100); 
// Why 100?= just to be safe.  But still not working.

Now, this function copys and stores the first char[254] of the pointer a into 1D array curr but the rest of the variables of the typedef struct is NULL.

Any advice?

(Using VS2012 on Windows)

typedef struct nodebase{
    char productname[254];
    char companyname[254];
    int price;
    int stocks;
//....
    struct nodebase *next; //Use the struct as linked-list
}item;
8
  • @ZacWrangler Fixed! Typo Commented Oct 4, 2013 at 4:31
  • @BeginnerC now think about what sizeof(item*) is. It isn't what you want. Also, this most certainly is a duplicate. Commented Oct 4, 2013 at 4:32
  • possible duplicate of memcpy a buffer and an array not working Commented Oct 4, 2013 at 4:34
  • @H2CO3 Is it just the same thing when I am using typedef struct? Just asking Commented Oct 4, 2013 at 4:36
  • @beginnerC: yes, is the exact same thing: a failure in providing memcpy() with the correct size. Commented Oct 4, 2013 at 4:37

1 Answer 1

1

Consider what the code fragment does,

  • function returns void/nothing

    void
    
  • function name is pointer_conversion, takes three arguments

  • argument a is a pointer to item, (item*)
  • argument curr is an array of item, (item[10000])
  • argument total is an int

    pointer_conversion(item *a, item curr[10000], int total)
    {
    
  • memcpy takes three arguments, destination, source, and number of bytes to copy

  • how big is sizeof(item*)? it is as big as a pointer.
  • how many bytes do you want to copy? how big is sizeof(item)?

    memcpy(&curr[total], a, sizeof(item*));
    }
    
  • but you probably don't want to copy the item* next element of the item

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! I got misleaded. ^^

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.