Home > database >  How can I Initialize a global 1D array of type char with a number?
How can I Initialize a global 1D array of type char with a number?

Time:12-15

Initialize a global 1D array "StudentData" of type char with your ID (5 digits). Using pointers syntax is mandatory in this part. Can you give me some tips about doing that?

I tried doing this,

char *StudentData;
void loadData(){
        StudentData=(char*)"60897";
}

Is it right or should I try doing something else?

CodePudding user response:

You probably want something along the lines of:

#include <string.h>  //This header has strcpy()

#define ENOUGH_DIGITS   5 //This is so you can easily modify ID length in the future

char StudentData[ENOUGH_DIGITS 1];  //Global array 1 bigger than the longest string

void loadData(){
   //Ask the compiler to put a read-only string somewhere in the data memory
   const char *myID = "60897";  
   //Copy the read-only string into the global array
   strcpy(StudentData,myID);
}

CodePudding user response:

1D array "StudentData"

No no no. StudentData is not an array, it's a pointer. Arrays are blocks of memory while pointers are addresses to memory, which may or may not be an array. An array sometimes becomes a pointer, that's called decay.

"60897" is already a char * or rather say a string literal. You can directly assign it to StudentData. Like this:

StudentData = "60897";

If you want to use an array, do this:

#include <string.h>
char StudentData[WHATEVER_IS_ENOUGH_TO_HOLD_THE_DATA]; /* allocate array.
 make sure that the size of the array accounts for the null 
terminating character. */
void loadData(){
        strcpy(StudentData, "60897"); //you can't directly assign to an array.
}

CodePudding user response:

While you are indeed using an array (the string literal actually is one), it is an anonymous one – and your studentData is only a pointer to that one.

a global 1D array "StudentData"

I would rather interpret this as you are intended to have a true array with that name, so that would look like:

char studentData[N];

where N is a constant expression representing an appropriate size for your array, at least 5 as you need to be able to store 5 digits – possibly 6 if your ID should be represented as a C-string (such one needs one additional character holding the mandatory null-terminator!), or you go with a power of two right away (8 minimally).

Using pointers syntax is mandatory in this part.

So you'll need a pointer to that array:

char* ptr = studentData;

You could now just use the pointer to assign values to:

*ptr   = '0'; // dereferences the pointer, assigns a value to (in this
              // case a character representing the digit zero) and
              // increments it afterwards to point to the next character

// repeat this for the next four digits!

*ptr   = 0; // terminating null character (note: no single quotes)
// or:
*ptr = 0;
// it's up to you to decide if incrementing yet another time actually
// is meaningful (first variant) or unnecessary (second variant)...

If there are no further requirements given you might have this code directly in main function or as a little bonus place it in another function being called from main like:

void loadData(size_t size, char data[size])
{
    // ideally size check with appropriate error handling

    // assignment as above
}

// in main:
loadData(sizeof(studentData), studentData);

Note: For function parameters all of char* data, char data[] or char[someArbitrarySize] are equivalent, if any size is given, it is simply ignored – we still can add it for documentation purposes, in above signature: to tell that an array with a size of (at least) size is expected. Note, too, that if there are more than one dimensions given this only applies for the outer most dimension, though!

  • Related