CODE_TOLD_FAST

FACEBOOK/STRING_VS_STRING

May 1st, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.70 KB | None | 0 0
  1.  
  2.     //:SVS:String_Vs_String
  3.     //:PASTEBIN_URL_TO_THIS_FILE: https://pastebin.com/PqKSz4GN
  4.  
  5.     #include <stdio.h> //:for: printf(...)
  6.     #include<stdlib.h> //: <-- for malloc(...)
  7.  
  8.     //: https://en.cppreference.com/w/c/language/operator_precedence
  9.     //: You were correct:
  10.     //: [] binds tighter than &
  11.     //:
  12.     //: But I just realized that the effective result
  13.     //: of both expressions is the same.
  14.  
  15.  
  16.     void TotallyNot_clGetPlatformInfo(
  17.         void* str_out
  18.     )
  19.     {
  20.         static int times_called=0;
  21.         times_called++;
  22.  
  23.         char* str=(char*)str_out;
  24.  
  25.         //:Setting different results for each pair
  26.         //:just to verify we are in fact working with
  27.         //:different memory addresses.
  28.  
  29.         if( 1 *2 >= times_called ){
  30.             str[0]='A';
  31.             str[1]='B';
  32.             str[2]='C';
  33.             str[3]=( 0 ); //:null_terminator.
  34.         }else
  35.         if( 2 *2 >= times_called ){
  36.             str[0]='D';
  37.             str[1]='E';
  38.             str[2]='F';
  39.             str[3]=( 0 ); //:null_terminator.
  40.         }else
  41.         if( 3 *2 >= times_called ){
  42.             str[0]='G';
  43.             str[1]='H';
  44.             str[2]='I';
  45.             str[3]=( 0 ); //:null_terminator.
  46.         }else
  47.         if( 4 *2 >= times_called ){
  48.             str[0]='J';
  49.             str[1]='K';
  50.             str[2]='L';
  51.             str[3]=( 0 ); //:null_terminator.
  52.         }else
  53.         if( 5 *2 >= times_called ){
  54.             str[0]='M';
  55.             str[1]='N';
  56.             str[2]='O';
  57.             str[3]=( 0 ); //:null_terminator.
  58.         }else
  59.         {
  60.             str[0]='_';
  61.             str[1]='_';
  62.             str[2]='_';
  63.             str[3]=( 0 ); //:null_terminator.
  64.         };;
  65.     }
  66.  
  67.  
  68. int main( void )
  69. {
  70.     printf("[BEG:FILE:whatever:FUNC:main]\n");
  71.    
  72.  
  73.     { //:SCOPE
  74.  
  75.         char  str_sta_01[256]={0};
  76.         char* str_hea_01 = malloc( 256 );
  77.  
  78.         char  str_sta_02[256]={0};
  79.         char* str_hea_02 = malloc( 256 );
  80.  
  81.         char  str_sta_03[256]={0};
  82.         char* str_hea_03 = malloc( 256 );
  83.  
  84.         char  str_sta_04[256]={0};
  85.         char* str_hea_04 = malloc( 256 );
  86.  
  87.         char  str_sta_05[256]={0};
  88.         char* str_hea_05 = malloc( 256 );
  89.  
  90.  
  91.         TotallyNot_clGetPlatformInfo(  str_sta_01 );
  92.         TotallyNot_clGetPlatformInfo(  str_hea_01 );
  93.  
  94.     //: WRONG unless function is emulating passing by
  95.     //: reference by taking a void** (double void pointer)
  96.     //: TotallyNot_clGetPlatformInfo(  &str_sta_01 );
  97.     //: TotallyNot_clGetPlatformInfo(  &str_hea_01 );
  98.  
  99.         //:Equivalent end results: ( I did not realize this )
  100.         //:Both of these get the base address of array.
  101.         ////////////////////////////////////////////////////////
  102.  
  103.             //:De-reference first element, then take address.
  104.             //:Effectively: Address of head of array.
  105.             TotallyNot_clGetPlatformInfo( &(str_sta_02[0]) );
  106.             TotallyNot_clGetPlatformInfo( &(str_hea_02[0]) );
  107.  
  108.             //:Get double pointer (char**) then de-reference
  109.             //:at the base using [0], getting char*
  110.             TotallyNot_clGetPlatformInfo( (&str_sta_03)[0] );
  111.             TotallyNot_clGetPlatformInfo( (&str_hea_03)[0] );
  112.  
  113.             //:For good measure, let's write the above
  114.             //:two examples slightly differently:
  115.             ////////////////////////////////////////////////////
  116.  
  117.                 //:Dereference, then take address:
  118.                 TotallyNot_clGetPlatformInfo( &str_sta_04[0] );
  119.                 TotallyNot_clGetPlatformInfo( &str_hea_04[0] );
  120.  
  121.                 //:Take address, then dereference:
  122.                 TotallyNot_clGetPlatformInfo( &(str_sta_05)[0]);
  123.                 TotallyNot_clGetPlatformInfo( &(str_hea_05)[0]);
  124.  
  125.             ////////////////////////////////////////////////////
  126.  
  127.  
  128.         ////////////////////////////////////////////////////////
  129.        
  130.         printf( "[str_sta_01]:%s\n", str_sta_01 );
  131.         printf( "[str_hea_01]:%s\n", str_hea_01 );
  132.  
  133.         printf( "[str_sta_02]:%s\n", str_sta_02 );
  134.         printf( "[str_hea_02]:%s\n", str_hea_02 );
  135.  
  136.         printf( "[str_sta_03]:%s\n", str_sta_03 );
  137.         printf( "[str_hea_03]:%s\n", str_hea_03 );
  138.  
  139.         printf( "[str_sta_03]:%s\n", str_sta_04 );
  140.         printf( "[str_hea_03]:%s\n", str_hea_04 );
  141.  
  142.         printf( "[str_sta_03]:%s\n", str_sta_05 );
  143.         printf( "[str_hea_03]:%s\n", str_hea_05 );
  144.        
  145.  
  146.         free( str_hea_01 );
  147.         free( str_hea_02 );
  148.         free( str_hea_03 );
  149.         free( str_hea_04 );
  150.         free( str_hea_05 );
  151.     } //:SCOPE
  152.  
  153.  
  154.     printf("[END:FILE:whatever:FUNC:main]\n");
  155. }
  156.  
  157. //:Bash( GitBash ) build script for this code:
  158. /**-********************************************************-**+
  159.  
  160.     ##  SC[ hkmf-mini] #########################################
  161.     ## SEE[ hkmf-c11 ] for reference:  #########################
  162.     ############################################################
  163.     gcc                                                        \
  164.         -x c                                                   \
  165.         -c "SVS.C11"                                           \
  166.         -o object_file.o                                       \
  167.                                                                \
  168.             -Werror                                            \
  169.             -Wfatal-errors                                     \
  170.             -Wpedantic                                         \
  171.             -Wall                                              \
  172.             -Wextra                                            \
  173.                                                                \
  174.             -fstrict-aliasing                                  \
  175.             -Wstrict-aliasing                                  \
  176.                                                                \
  177.             -D MACRO_COMPILE_ONLY_THIS_FILE_AS_DEMO            \
  178.                                                                \
  179.             -std=c11                                           \
  180.             -m64 ###############################################
  181.                                     ####                    ####
  182.     gcc -o EXE.exe object_file.o    ####                    ####                
  183.     rm             object_file.o    ####                    ####    
  184.          ./EXE.exe                  ####                    ####
  185.     rm     EXE.exe                  ####                    ####
  186.                                     ####                    ####
  187.     read -p "[ENTER_TO_EXIT]:"      ####                    ####
  188.     ############################################################
  189.  
  190. +**-********************************************************-**/
Add Comment
Please, Sign In to add comment