Brian Jarcus
Brian Jarcus

Reputation: 69

Passing Argument 1 makes integer from pointer without a cast warning

I am using CodeBlocks and learning C. I created this simple script as a point for learning functions. I am not understanding the error I am getting though as everything matches up in my eyes.

CODE:

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

void SetPerson(char a, int b);

int main () {

    char name[50];
    int number[6];

    printf("Enter Name: ");
    scanf("%49s", name);

    printf("Enter Number: ");
    scanf("%5d", number);

    SetPerson(name, number);

    return(0);
} 

void SetPerson(char a, int b) {
    printf("Name: %s\n", a);
    printf("Number: %d", b);
}

In the compiler I am getting these errors:

||=== Build: Debug in remove (compiler: GNU GCC Compiler) ===|
C:\Users\e\Desktop\c programs\remove\main.c||In function 'main':|
C:\Users\e\Desktop\c programs\remove\main.c|17|warning: passing argument 1 of 'SetPerson' makes integer from pointer without a cast [-Wint-conversion]|
C:\Users\e\Desktop\c programs\remove\main.c|4|note: expected 'char' but argument is of type 'char *'|
C:\Users\e\Desktop\c programs\remove\main.c|17|warning: passing argument 2 of 'SetPerson' makes integer from pointer without a cast [-Wint-conversion]|
C:\Users\e\Desktop\c programs\remove\main.c|4|note: expected 'int' but argument is of type 'int *'|
C:\Users\e\Desktop\c programs\remove\main.c||In function 'SetPerson':|
C:\Users\e\Desktop\c programs\remove\main.c|23|warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat=]|
||=== Build finished: 0 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|

EDIT:

I changed as recommend:

SetPerson(char *a, int b);

And now I am at these errors:

||=== Build: Debug in remove (compiler: GNU GCC Compiler) ===|
C:\Users\e\Desktop\c programs\remove\main.c||In function 'main':|
C:\Users\e\Desktop\c programs\remove\main.c|17|warning: passing argument 2 of 'SetPerson' makes integer from pointer without a cast [-Wint-conversion]|
C:\Users\e\Desktop\c programs\remove\main.c|4|note: expected 'int' but argument is of type 'int *'|
||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

Upvotes: 1

Views: 30862

Answers (2)

CoderOmkar
CoderOmkar

Reputation: 1

Why are you using an array for an integer if you just want to store a single integer? If you want to store five integers then you're doing it incorrectly. If you want to store all five integers, then apply a for or while loop for scanning each number:

int number[5];

for(int i = 0; i < 5 ; i++)
{
     scanf("%d", number[i]);
}

In your code, scanf("%5d", number); would just store any number up to five digits, not those 5 digits separately.

Now the thing left about passing those arrays to another functions. Here you have to pass the addresses of those arrays as SetPerson(name, number);, and in the formal arguments you have to define pointers for the respective variables such as

SetPerson( char *a, int *b )

Inside this function you're printing the string in a correct way, but not the integers (if you wanna print all the 5 integers in separately). For the condition I've written inside the parenthesis, you must apply a while or a for loop to print those integers separately.

Upvotes: 0

nemequ
nemequ

Reputation: 17482

name is an array of characters, but the first argument to SetPerson is a single character. Change SetPerson to

void SetPerson(char* a, int b) {
    printf("Name: %s\n", a);
    printf("Number: %d", b);
}

Note that in C, arrays and pointers are basically interchangeable.

Edit (after question was modified)

You're basically doing the same thing in reverse with the number. You have an array of ints (basically int* which you're passing to a int argument.

Drop the array portion of the declaration for number; you're not telling the compiler to allocate a 6-digit (in base 10) number, you're telling it to allocate 6 32-bit (probably; int is generally 32-bit these days, but may not be) numbers.

Then you'll need to change the arguments to scanf to pass a pointer to number instead of the actual number; use the address-of operator (&) for that:

scanf("%d", &number);

Upvotes: 5

Related Questions