{"id":1109,"date":"2010-07-23T09:47:17","date_gmt":"2010-07-23T09:47:17","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=1109"},"modified":"2011-06-07T09:48:22","modified_gmt":"2011-06-07T08:48:22","slug":"struct-setup-and-memory-locations","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/07\/23\/struct-setup-and-memory-locations\/","title":{"rendered":"struct &#8211; setup and memory locations"},"content":{"rendered":"<p>When you setup a struct within c++, it is kinder like having a array of data and if you want to you can access the internal parts by using some memory pointer location fun!.<\/p>\n<p>Lets say that you have a struct of <\/p>\n<pre lang=\"cpp\">\r\nstruct intvalue {\r\n    int a;\r\n    int b;\r\n};<\/pre>\n<p>Just to say that since I am using int(eger) values so that is what I am incrementing by in the pointer arithmetic which is why I am casting the pointer to a integer value.<\/p>\n<p>So lets say that we create a variable of intvalue and setup the values as below<\/p>\n<pre lang=\"cpp\">\r\nintvalue testvalue;\r\ntestvalue.a = 4;\r\ntestvalue.b = 5;<\/pre>\n<p>We can then pull out the value of a or b, but using memcpy and just outputting to a int(eger) variable as below, the key is the ((int*)&#038;testvalue)+1, this will first convert the testvalue variable to a pointer to memory location and then (int*) casts that pointer to a int pointer, because internally that is what it is, and then just add 1 to it, which points to the second value ( in this case the value of b which is 5)<\/p>\n<pre lang=\"cpp\">\r\n    int avalue;\r\n    \/\/ convert to a int pointer type and then add one to it (to the next array element as such).\r\n    memcpy(&avalue, ((int*)&testvalue)+1,sizeof(int));\r\n    cout << \"a value (or is it the b value :) ) \" << avalue << endl;\r\n<\/pre>\n<p>The output would be <\/p>\n<pre lang=\"bash\">\r\na value (or is it the b value :) ) 5\r\n<\/pre>\n<p>because I am pointing to the second value which is b and thus 5 :).<\/p>\n<p>Of course if you just wanted the value of first int (the value of a in this case) you do not add the 1 to the memory location, for example<\/p>\n<pre lang=\"cpp\">\r\n    memcpy(&avalue, ((int*)&testvalue),sizeof(int));\r\n<\/pre>\n<p>this time I am just converting the testvalue (casting) to a int pointer and thus pointing to the start of the struct and that is where the int a variable is living :).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you setup a struct within c++, it is kinder like having a array of data and if you want to you can access the internal parts by using some memory pointer location fun!. Lets say that you have a struct of struct intvalue { int a; int b; }; Just to say that since &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/07\/23\/struct-setup-and-memory-locations\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">struct &#8211; setup and memory locations<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[131,104],"class_list":["post-1109","post","type-post","status-publish","format-standard","hentry","category-c_and_cpp","tag-memcpy","tag-struct"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=1109"}],"version-history":[{"count":3,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1109\/revisions"}],"predecessor-version":[{"id":1624,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1109\/revisions\/1624"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=1109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=1109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=1109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}