1

I am a complete novice in C, but I have been assigned for the work to understand the C code. Can anyone help understand the following code?

memcpy (out, &field_id, sizeof(field_id));

Here are the datatypes of field_id and out:

char* out = *out_ptr;
short field_id = column->column_index;

As far as I can understand the command is copying sizeof(field_id) number of characters from &field into out pointer. But this does not make any sense to me. Also, as far as I know C is not an OOP language, how come we are using something like column->column_index which seems like we are trying to access column_index property of some object named column.

I have advanced programming experience in Java, Python, C# so I can understand even if you don't go in much details, only hints or links to helpful articles will also do.

Ilya
  • 4,583
  • 4
  • 26
  • 51
n00b
  • 214
  • 1
  • 12
  • 1
    you could start looking into the definition of the column variable, which is apparently a struct (->: accessor for field) and is the closest abstraction to class you will get in C. – toine Jul 02 '14 at 11:18
  • 1
    C may not be object oriented, but that doesn't mean it can't have data types with multiple child members. In fact, it's quite common to do OO programming in plain C, although it's considerably clunkier than it would be in a 'real' OO language. – ams Jul 02 '14 at 11:46
  • @smagnan: Do you think that I will post a question on SO without even doing some research?I looked at this http://www.tutorialspoint.com/c_standard_library/c_function_memcpy.htm and several other sources before coming to SO. If you are only concerned about the fact whether I have checked official documentation or not,then my answer would be no. – n00b Jul 02 '14 at 11:48
  • @user3336236 Sorry, didn't mean to offend, but I have seen many people asking about how to use functions (etc ...) without even checking anything about it before, that's why I asked ... – smagnan Jul 02 '14 at 12:31
  • @smagnan: That's okay buddy.I am a long time user of Stack Overflow and I know how this thing goes.I am just unable to use my regular SO account because I can't log into SO using fb from my workplace,LOL. – n00b Jul 02 '14 at 13:30

2 Answers2

6

Copying raw bytes all around the place is part of what makes C fun. And somewhat dangerous, and prone to bugs.

But, without more context it's impossible to say what the copying achieves. It can be some form of serialization, copying the bytes that make up the field_id integer into some character buffer at out, perhaps for transmission or saving to disk.

The the -> operator is used to access members (fields) of C's structure values (structs, very very common) when you have a pointer to the struct. Basically for any pointer a to a struct, the expression a->b is the same as (*a).b, where the prefix * operator is C's pointer-dereferencing operator.

unwind
  • 391,730
  • 64
  • 469
  • 606
5

This statement copies internal representation of integer field_id of type short into the buffer pointed to by out

memcpy (out, &field_id, sizeof(field_id));

You could write it also as

memcpy (out, &field_id, sizeof( short ));

As for this statement

short field_id = column->column_index;

then column can be a pointer to some structure (or union) that has member named column_index. For example

struct COLUMN
{
   short column_index;
} *column = ( struct COLUMN * )malloc( sizeof( struct COLUMN ) );

column->column_index = 10;

In C++ structures are classes with keyword struct So the syntax to access members of structures (or classes in C++) is the same in C and C++.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • That `malloc()` line is rather complicated-looking, it should just be `} *column = malloc(sizeof *column);` really. [Please don't cast the return value of `malloc()` in C](http://stackoverflow.com/a/605858/28169). – unwind Jul 02 '14 at 12:09