0

I have to inizialize a char array [DIM_MAX] to a null value in order to pass it to another function. Before passing it to this function, I am trying to print it as a local variable in order to understand if it assumes a value of zero, but unfortunately this does not happen.

I did:

 static char bufferW[DIM_WRITE];

Then I wanted to inizialize this buffer to zero. Is correct to do this?

*bufferW="NULL";

I have to pass it to the function

write(0x02900800, bufferW, DIM_WRITE);

but bufferW isn't passed as a NULL char, it is the last value assumed. Where am I doing wrong? I tried:

bufferW==NULL;

is the way? I am sorry for the basic question. Any help will be appreciated

kind regards

kevin94
  • 37
  • 5
  • As an object with `static` storage class and no initializer - it is already initialized to all zeros. But from your attempts it looks like you might not be having the basics to do the things you are trying to do... – Eugene Sh. Apr 07 '22 at 17:49
  • Are you trying to set your string to "NULL" or the string pointer to NULL? – Andrej Prsa Apr 07 '22 at 17:54
  • 5
    You seem to be mixing up null pointers, the numerical value null (zero) and the string literal `"NULL"` – looks pretty much as if you are in urgent need of a [good C book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list)... – Aconcagua Apr 07 '22 at 17:54
  • I am so sorry, but I cannot use strcpy...how can I initialize a string to null? If I write NULL in the write function it works – kevin94 Apr 07 '22 at 18:02
  • that string is initialized to null already because you declared it static – pm100 Apr 07 '22 at 18:05
  • also note that `==` is the comparison operator, `=` is the assignment operator. But neither makes sense in this context for `bufferW=NULL` or `bufferW==NULL`. `bufferW` is a static array, not a pointer. It exists in the same place in memory for the duration of the program. Your insistence that `bufferW` is NULL (in whatever sense) is also unclear. Most assuredly the `write` function is expecting a pointer to a block of data of a specified length (`DIM_WRITE` in your case). Passing it a NULL pointer will at best cause nothing to happen, and at worse invoke undefined behavior. – yano Apr 07 '22 at 18:22
  • if you want `bufferW` to be initialized with the string `"NULL"`, then you can simply do `static char bufferW[DIM_WRITE] = "NULL";`, assuming that `DIM_WRITE >= 5`, but based on the other parts of your question, probably not what you want. – yano Apr 07 '22 at 18:24
  • Ok thanks to all, I am Sorry but i am not a programmer – kevin94 Apr 07 '22 at 18:37

1 Answers1

0

bufferW is an array, which takes up DIM_WRITE bytes.

If you're trying to set every byte in the array to 0, the function you want to call is

memset(bufferW, 0, DIM_WRITE); //Make sure to #include <string.h>

If you don't have access to the functions prototyped in string.h, you can also overwrite the buffer with a simple loop such as

for(int i = 0; i < DIM_WRITE; ++i)
  bufferW[i] = 0;

You can do this at any time in your program to overwrite the values in bufferW, but if you haven't written anything to the buffer yet, the static scope specifier in the declaration ensures it will be initialized to zero at the start of your program.

To write the string "NULL" in the buffer you can

strcpy(bufferW,"NULL"); //Make sure to #include <string.h>

Or manually write each character in the buffer

bufferW[0] = 'N';
bufferW[1] = 'U';
bufferW[2] = 'L';
bufferW[3] = 'L';
bufferW[4] = '\0'; //Null terminator

But for these to work DIM_WRITE must be at least 5.

Willis Hershey
  • 1,520
  • 4
  • 22