1

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

typedef struct {
  int iNum;
  int base;
  char* sNum;
} number;

number * intToNumber(int nnum, int nbase) {
  number * theNumbers;

  //theNumbers= (number*)malloc(10*sizeof(number));

  if (theNumbers = NULL) {
    printf("nnNonn");
    exit(1);
  }

  theNumbers->iNum = nnum;
  theNumbers->base = nbase;

}
|

3

1

typedef struct {
    int iNum;   /* automatic storage types */
    int base;
    char *sNum; /* a pointer - hold address to something else  */
} number;       /*             must point to valid memory addr */

506

number *intToNumber (int nnum, int nbase) {

    number *theNumbers;

    theNumbers= malloc (sizeof *theNumbers);    /* allocate */

    if (theNumbers == NULL) {                   /* validate */
        perror ("malloc-theNumbers");
        return NULL;
    }

    theNumbers->iNum = nnum;    /* initialize values */
    theNumbers->base = nbase;
    theNumbers->sNum = NULL;

    return theNumbers;          /* return pointer */
}

/* allocate/set sNum member of n to s */
char *setsNum (number *n, const char *s)
{
    if (!n) {   /* validate n not NULL, return NULL on failure */
        fputs ("error: struct parameter 'n' NULL.n", stderr);
        return NULL;
    }
    size_t len = strlen (s);        /* get length */

    n->sNum = malloc (len + 1);     /* allocate storage (+1 byte) */

    if (!n->sNum) {                 /* validate allocation */
        perror ("malloc-sNum");
        return NULL;
    }

    memcpy (n->sNum, s, len + 1);   /* copy s to new block of memory */

    return n->sNum;                 /* return pointer (convenience) */
}

int main (void) {

    number *mynum = intToNumber (50, 6);    /* declare/initialize mynum */

    if (!mynum)     /* validate succeeded */
        return 1;

    /* allocate/validate mynum->sNum, copy string */
    if (!setsNum (mynum, "created with intToNumber (50, 6)")) {
        free (mynum);
        return 1;
    }
    /* output values held in mynum */
    printf ("succeeded:n iNum: %dn base: %dn str : %sn",
            mynum->iNum, mynum->base, mynum->sNum);

    free (mynum->sNum); /* free allocated string */
    free (mynum);       /* free struct */
}

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

typedef struct {
    int iNum;   /* automatic storage types */
    int base;
    char *sNum; /* a pointer - hold address to something else  */
} number;       /*             must point to valid memory addr */

number *intToNumber (int nnum, int nbase) {

    number *theNumbers;

    theNumbers= malloc (sizeof *theNumbers);    /* allocate */

    if (theNumbers == NULL) {                   /* validate */
        perror ("malloc-theNumbers");
        return NULL;
    }

    theNumbers->iNum = nnum;    /* initialize values */
    theNumbers->base = nbase;
    theNumbers->sNum = NULL;

    return theNumbers;          /* return pointer */
}

/* allocate/set sNum member of n to s */
char *setsNum (number *n, const char *s)
{
    if (!n) {   /* validate n not NULL, return NULL on failure */
        fputs ("error: struct parameter 'n' NULL.n", stderr);
        return NULL;
    }
    size_t len = strlen (s);        /* get length */

    n->sNum = malloc (len + 1);     /* allocate storage (+1 byte) */

    if (!n->sNum) {                 /* validate allocation */
        perror ("malloc-sNum");
        return NULL;
    }

    memcpy (n->sNum, s, len + 1);   /* copy s to new block of memory */

    return n->sNum;                 /* return pointer (convenience) */
}

int main (void) {

    number *mynum = intToNumber (50, 6);    /* declare/initialize mynum */

    if (!mynum)     /* validate succeeded */
        return 1;

    /* allocate/validate mynum->sNum, copy string */
    if (!setsNum (mynum, "created with intToNumber (50, 6)")) {
        free (mynum);
        return 1;
    }
    /* output values held in mynum */
    printf ("succeeded:n iNum: %dn base: %dn str : %sn",
            mynum->iNum, mynum->base, mynum->sNum);

    free (mynum->sNum); /* free allocated string */
    free (mynum);       /* free struct */
}
|
    0

    |
      0

      int main ()
      {
          number Numbers = {.sNum = malloc (25) };
      
          // Do stuff here...
          intToNumber(&Numbers);
      
          // Do some more stuff...
      }
      

      |