Thread: warning assignment makes interger from pointer without cast

  1. #16
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    but here is the rub .....
    if i do this
    Code:
    int main()
    {
        int i;
        char mystring[2][10] = {"hello you", "bye you"};
    
        for (i = 0; i <2; i++)
        {
           printf("%s\n", mystring[i]);
        }
    }
    its as happy as anything and i get the obvious output
    however if i then change it to this
    Code:
    int main()
    {
        int i;
        char mystring[2][10] = {"hello you", "bye you"};
    
        for (i = 0; i <2; i++)
        {
            printf("enter upto 8 characters");
            scanf(" %s", &mystring[i]);
        }
    }
    i get more warnings than i know what to do with
    coop

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What are the warnings? What %s with scanf expect?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    format %s expects argument of type char * but argument 2 has type char [*][10]

    if i change mystring[2][10] to *mystring[2][10] i get the same warning except its char [**][10]

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cooper1200
    if i change mystring[2][10] to *mystring[2][10]
    Why did you do that? This is very important. You cannot program blindly. So why?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    because it wanted a pointer right???

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cooper1200
    because it wanted a pointer right???
    Sure, so you randomly insert a & here or a * there and maybe the stars will align and the code will compile and run with the correct result. What are you, an alchemist? I suggest adding heartsbane of great silver wolf to get "the pointer". Remember to stir the cauldron exactly 47 times, clockwise.

    No, you know that it wants a char*. You also know that in most contexts, an array of char is converted to a pointer to its first element. So, figure it out. Stop guessing.

    Quote Originally Posted by cooper1200
    if i change mystring[2][10] to *mystring[2][10] i get the same warning except its char [**][10]
    Wait, you mean you changed the declaration of mystring? Look, you need to think, not change the code blindly hoping that it'll work.

    Quote Originally Posted by cooper1200
    format %s expects argument of type char * but argument 2 has type char[*][10]
    This is definitely not the error message. Argument 2 has type char (*)[10], i.e., pointer to an entire array of 10 char.

    Go back to the basics. Write a program that requests for the user's name and reads it as a string into an array of char using scanf and %s, then prints out a hello greeting with the user's name. This is like the second or third program beginners might write, and you need to be able to write it correctly before moving on to 2D arrays.
    Last edited by laserlight; 05-04-2019 at 07:54 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    that is exactly what i was trying to do when i got he error message it said and u said it wanted a pointer so it wasn't blind faith i gave it a pointer and it still wasn't happy

  8. #23
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    also where in the above examplles have i decalered any strings as type int

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cooper1200
    that is exactly what i was trying to do when i got he error message it said and u said it wanted a pointer so it wasn't blind faith i gave it a pointer and it still wasn't happy
    That's like saying "the cab driver wanted US dollar, so I gave them US dollar, but they still weren't happy". Of course they weren't happy! You were supposed to pay 50 USD for the cab fare, not 5 USD! You have to pay attention to the pointer type, not just say "I'm passing a pointer when a pointer is needed, so it is alright". Passing a char(*)[10] when a char* is expected doesn't cut it.

    Quote Originally Posted by cooper1200
    also where in the above examplles have i decalered any strings as type int
    You passed a char as an argument, and this was promoted to int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    i can be so thick at times i need shot
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void put_info(char names[][4][20], char info[], int i, int j);
    void get_info(char names [][4][20], int i, int j);
    
    int main()
    {
        int i, j;
        char names[2][4][20];
    
        for (j = 0; j < 2; j++)
        {
            for (i = 0; i < 4; i++)
            {
                get_info(names, i, j);
            }
        }
    
        for (j = 0; j < 2; j++)
        {
            for (i = 0; i < 4; i++)
            {
                printf("%s\t\t\t",  names[j][i]);
            }
            printf("\n");
        }
    }
    
    void get_info(char names [][4][20], int i, int j)
    {
        char info[20];
    
        switch(i)
        {
            case 0:
                printf("enter first name: ");
                scanf(" %s", info);
                break;
            case 1:
                printf("enter second name: ");
                scanf(" %s", info);
                break;
            case 2:
                printf("enter address: ");
                scanf(" %s", info);
                break;
            case 3:
                printf("enter phone number: ");
                scanf(" %s", info);
                break;
        }
        put_info(names, info, i, j);
    
    }
    
    void put_info(char names[][4][20], char info[], int i, int j)
    {
    
        strcpy(names[j][i], info);
    }
    coop

  11. #26
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    how many times have i been told and read that when dealing with strings the variable name is the pointer so a call of scanf doesn't need another pointer the variable name is the pointer ie variable_name rather than & variable_name.

    When passing arrays the left most size is omitted in the function deceleration ie

    myarray[] if 1d myarray[][2] for 2d and myarray[][2][4] for 3d array etc.

    Please feel free to beat me round the head with a 2x4 next time.

    thanks for all your patience
    coop

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Good to see that you've got it.

    By the way, I would avoid 3D arrays unless I really have to have them: consider introducing a struct as an abstraction, e.g.,
    Code:
    struct Contact
    {
        char first_name[20];
        char second_name[20];
        char address[20];
        char phone_number[20];
    };
    
    // ...
    
    struct Contact contacts[2];
    You might no longer be able to do your indexing trick with put_info and get_info, but it'll be easier to understand and hence easier to debug and maintain in the long run.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-09-2014, 09:19 PM
  2. Replies: 4
    Last Post: 03-03-2010, 01:06 PM
  3. Replies: 1
    Last Post: 12-23-2008, 09:39 AM
  4. Replies: 17
    Last Post: 02-13-2006, 01:19 PM
  5. Replies: 3
    Last Post: 01-14-2002, 12:13 PM

Tags for this Thread