3

I am working on an old C code repo that has some weird syntax that I have never seen.

char (*__kaboom)[NUM_ELEMS( Port ) ] = NULL;

How does this work? what does __kaboom do and how can we use it say for example in a print statement?

And other places like these

char (*name)[MAX] = NULL;
name = (char (*)[MAX]) malloc(MAXB*sizeof(char));

I am new to C programming and appreciate your suggestion. Thanks

Red Gundu
  • 183
  • 2
  • 10
  • Found answer for __kabbom here https://stackoverflow.com/questions/20979565/how-can-i-print-the-result-of-sizeof-at-compile-time-in-c/35261673#35261673 Thanks – Red Gundu Jan 21 '21 at 19:44

3 Answers3

4

Most likely, NUM_ELEMS(Port) is a macro that expands to (sizeof(Port)/sizeof(Port[0])) or something similar. If so, it only works properly when the argument is the name of an array defined in the source file (not declared extern and defined in another file) that is not a function parameter. Whatever it is, it must expand to an integer constant, or to nothing.

The notation:

char (*variable)[SIZE] = NULL;

is declaring a pointer to an array of the given size and type, and setting the pointer to NULL.

The definition and cast in the second fragment are conceptually similar.

The name __kaboom is reserved for the implementation to use. Note that you should not, in general, create function, variable, tag or macro names that start with an underscore. Part of C11 §7.1.3 Reserved identifiers says:

  • All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
  • All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.

See also What does double underscore (__const) mean in C?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • `It only works properly when the argument is an array that is not a function parameter` no : https://godbolt.org/z/nYaqG8 – 0___________ Jan 21 '21 at 18:45
  • @0: What does your link show? I don't go to random web sites, not even godbolt.com. What I said is a sufficiently good approximation to the truth for someone learning C, I believe. Unless you care to spell out in a comment exactly what your complaint is. – Jonathan Leffler Jan 21 '21 at 18:47
  • `NUM_ELEMS(Port)` does not have to be something you think – 0___________ Jan 21 '21 at 18:51
  • I said "likely" — that's my judgement call. You're right; it is also possible that I'm wrong. I'm not sure it warrants a comment, though. – Jonathan Leffler Jan 21 '21 at 18:52
  • 1
    Chuckling... Yes... in the world of possibilities it could be an array of constants, a lookup table, etc... which is why this site requires [A Minimal, Complete, and Verifiable Example (MCVE)](http://stackoverflow.com/help/mcve) (Advise: Don't tug on Superman's cape -- rarely beneficial...) – David C. Rankin Jan 21 '21 at 19:00
  • Thanks for the suggestion. I think i find the solution. https://stackoverflow.com/questions/20979565/how-can-i-print-the-result-of-sizeof-at-compile-time-in-c/35261673#35261673 – Red Gundu Jan 21 '21 at 19:30
  • 1
    The macro can work as long as the size of the array is known at the point of use. If the array is declared with a size it is OK, and if the array is declared without a size and defined in the same file, but after the point of use, it is not OK. – chqrlie Jan 21 '21 at 19:32
1

In this declaration

char (*__kaboom)[NUM_ELEMS( Port ) ] = NULL;

there is declared a pointer to an array of the type char[NUM_ELEMS( Port ) ].

Here is a demonstrative program.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) 
{
    char s[] = "Hello";
    
    char ( *p )[sizeof( s )] = NULL;
    
    p = ( char ( * )[sizeof( s )] )malloc( sizeof( char ) * sizeof( s ) );
    
    strcpy( *p, s );
    
    puts( *p ); 

    free( p );
    
    return 0;
}

The program output is

Hello

That is if you have an array like

char s[N];

then a pointer to such an array can be declared like

char ( *p )[N] = &s;

Or you can assign the address of a dynamically allocated memory for an array of the type char[N] (or char [MAX] as in your example) to the pointer

p = ( char ( * )[N] ) malloc( N * sizeof( char ) );

Dereferencing the pointer you will get the pointed array (lvalue).

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1
  1. define __kaboom as pointer to array of NUM_ELEMS( Port ) char elements and initialize it to NULL (BTW identifiers are not allowed to start with __)
  2. define name as pointer to array of MAX char elements and initialize it to NULL
  3. Assign pointer from the point 2. with reference to memory allocated by the malloc function.
0___________
  • 60,014
  • 4
  • 34
  • 74