1

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)) ===|
1
  • name is not a char. It's a pointer to a char (since it's an array of char), which is a char *`. Commented Jan 20, 2018 at 21:54

2 Answers 2

5

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);
Sign up to request clarification or add additional context in comments.

3 Comments

I see the error in my code now. The pointer should point to the start of the array. I have edited and still have a error that I am not sure about.
@BrianJarcus learn from the previous error. it's exactly the same error. number is an array of char, so it's a char *. But in this case, number should not be an array of int. You've declared it as int number[6] which is an array of six int values. It should just be an int. Why did you make it an array?
I may have been confused as I was learning and thought that was the max limit of what I wanted it to hold. I have taken a further look into the variables and I was confused. This has helped me clarify how these variables operate.
0

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.

Comments

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.