1

I have the following code that produces a warning stating "warning: initialization makes pointer from integer without a cast".

If I change the datatype *eleNum to char then it compiles cleanly. What am I doing wrong to cause the compilation to complain about *eleNum being an integer?

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

typedef struct ptElements {
    int  *eleNum;
    char *eleSym;
    char *eleNme;
    char *elePrd;
    char *eleGrp;
} Elements;

Elements database[] = { 
    { 1, "H",  "Hydrogen", "1", "1"  },  
    { 2, "He", "Helium",   "1", "18" },
    { 3, "Li", "Lithium",  "2", "1" },
    { 4, "Be", "Berylium", "2", "2" },
    { 5, "B",  "Boron",    "2", "13" }
};

int main() {
    int     i;  
    int     limit;

    limit = sizeof(database) / sizeof(Elements);

    for (i = 0; i < limit; i++)
        printf("%3s - %-3s %-20s %2s %2s\n", database[i].eleNum, database[i].eleSym, database[i].eleNme, database[i].elePrd, database[i].eleGrp);

    return 0;
}

struct-point-02.c:14:7: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
     { 1, "H",  "Hydrogen", "1", "1"  },
       ^
struct-point-02.c:14:7: note: (near initialization for ‘database[0].eleNum’)
struct-point-02.c:15:7: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
     { 2, "He", "Helium",   "1", "18" },
       ^
struct-point-02.c:15:7: note: (near initialization for ‘database[1].eleNum’)
struct-point-02.c:16:7: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
     { 3, "Li", "Lithium",  "2", "1" },
       ^
struct-point-02.c:16:7: note: (near initialization for ‘database[2].eleNum’)
struct-point-02.c:17:7: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
     { 4, "Be", "Berylium", "2", "2" },
       ^
struct-point-02.c:17:7: note: (near initialization for ‘database[3].eleNum’)
struct-point-02.c:18:7: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
     { 5, "B",  "Boron",    "2", "13" }
       ^
struct-point-02.c:18:7: note: (near initialization for ‘database[4].eleNum’)
1
  • 3
    Just use an integer when you should? int eleNum, it's an integer, not a pointer! Commented Nov 4, 2018 at 23:15

1 Answer 1

1

Main answer

Either use int eleNum in the structure, or use char *eleNum and change the initializer to use "1" for Hydrogen.

You currently print eleNum with %s — that's completely wrong (printing integer pointer as if it was a character pointer). Use %3d if eleNum becomes an int; use %3s if it becomes a char *.

If it remains an int *, you'll need to provide something like (int []){ 1 } (a C99 or later compound literal) as the initializer, and *database[i].eleNum or database[i].eleNum[0] in the printf() statement to print it, and use %3d.

#include <stdio.h>

typedef struct ptElements
{
    int   eleNum;
    char *eleSym;
    char *eleNme;
    char *elePrd;
    char *eleGrp;
} Elements;

Elements database[] =
{ 
    { 1, "H",  "Hydrogen", "1",  "1" },  
    { 2, "He", "Helium",   "1", "18" },
    { 3, "Li", "Lithium",  "2",  "1" },
    { 4, "Be", "Berylium", "2",  "2" },
    { 5, "B",  "Boron",    "2", "13" }
};

int main(void)
{
    int limit = sizeof(database) / sizeof(Elements);

    for (int i = 0; i < limit; i++)
    {
        printf("%3d - %-3s %-20s %2s %2s\n", database[i].eleNum,
               database[i].eleSym, database[i].eleNme, database[i].elePrd,
               database[i].eleGrp);
    }
    return 0;
}

Tangential information about the Periodic Table

I note that all the elements now have one-letter or two-letter symbols; the 3-letter symbols like Uub have all been replaced now with 'real' names. (See WebElements as one source of information — there are undoubtedly many others, such as Wikipedia Periodic Table of Elements.)

Back in 2010, I had an SQL table of elements with entries (the N indicates 'not stable' or 'radioactive'):

INSERT INTO elements VALUES(112, 'Uub', 'Ununbium',      277.0000, 'N');
INSERT INTO elements VALUES(113, 'Uut', 'Ununtrium',     284.0000, 'N');
INSERT INTO elements VALUES(114, 'Uuq', 'Ununquadium',   289.0000, 'N');
INSERT INTO elements VALUES(115, 'Uup', 'Ununpentium',   288.0000, 'N');
INSERT INTO elements VALUES(116, 'Uuh', 'Ununhexium',    293.0000, 'N');
INSERT INTO elements VALUES(118, 'Uuo', 'Ununoctium',    294.0000, 'N');

The first names for these were were added in mid-2010. By 2016, I had data equivalent to this (I added the period and group information in between the dates):

INSERT INTO elements VALUES(112, 'Cn',  'Copernicium',   285.1800, 7, '12', 'N');
INSERT INTO elements VALUES(113, 'Nh',  'Nihonium',      286.1800, 7, '13', 'N');
INSERT INTO elements VALUES(114, 'Fl',  'Flerovium',     289.1900, 7, '14', 'N');   -- Yes, Fl and Mc have the same weight
INSERT INTO elements VALUES(115, 'Mc',  'Moscovium',     289.1900, 7, '15', 'N');
INSERT INTO elements VALUES(116, 'Lv',  'Livermorium',   293.2000, 7, '16', 'N');
INSERT INTO elements VALUES(117, 'Ts',  'Tennessine',    293.2100, 7, '17', 'N');
INSERT INTO elements VALUES(118, 'Og',  'Oganesson',     294.2100, 7, '18', 'N');

It intrigues me to have lived through the 'completion' of the periodic table, at least for the first seven periods of it.

(You can find information about a hypothetical Ununennium (element 119, Uue) and Unbinilium (element 120, Ubn) on Wikipedia, if you're curious. There's also Beyond element 118: The next row of the Periodic Table.)

Sign up to request clarification or add additional context in comments.

1 Comment

Jonathan, many thanks for your solution(s) they worked perfectly. Thank you for taking the time to respond it is greatly appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.