के डेटा का आकार मैं क्रम में निम्न कोड की कोशिश की है कि कैसे एक सूचक की डेटा का आकार पाने के लिए देखने के लिए:हो रही है एक सूचक
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char *test_char_ptr = "This is just a test";
int *test_int_ptr = (int *) malloc(16*sizeof(int));
for(int i = 0; i<16; i++){
test_int_ptr[i] = i;
}
printf("%s\n",test_char_ptr);
printf("Test char 1: %d\n", (int)sizeof test_char_ptr);
printf("Test char 2:%d\n", (int)sizeof *test_char_ptr);
printf("Test char 3: %d\n", (int)((strlen(test_char_ptr)+1) * sizeof(char)));
printf("Test int 1:%d\n", (int)sizeof test_int_ptr);
printf("Test int 2:%d\n", (int)sizeof *test_int_ptr);
return EXIT_SUCCESS;
}
और कोड का उत्पादन होता है (32 बिट जीसीसी 4.3 पर) :
This is just a test
Test char 1: 4
Test char 2:1
Test char 3: 20
Test int 1:4
Test int 2:4
मुझे लगता है कि sizeof सोचा (* test_char_ptr) मेरे sizeof डेटा * test_char_ptr के अंदर देना होगा। लेकिन इसके बजाय यह मुझे 1 दिया, जो मुझे लगता है कि डेटा के बजाय आकार का आकार है। Test_int_ptr के लिए भी यही है। लंबी कहानी छोटी है, मेरा सवाल यह है कि मैं पॉइंटर या गतिशील रूप से स्मृति आवंटित सरणी के अंदर डेटा का आकार कैसे प्राप्त कर सकता हूं।
चूंकि आपके संपादन में स्ट्रेल() लाइन को बदलने के बाद से मुझे लगता है कि आउटपुट अब मेल नहीं खाता है। –
ठीक है मैंने आउटपुट अपडेट किया। – systemsfault
संभावित डुप्लिकेट [आकार को कैसे ढूंढें (एक सरणी को इंगित करने वाला पॉइंटर)] (http://stackoverflow.com/questions/492384/how-to-find-the-sizeofa-pointer-pointing-to-an- सरणी) – outis