C strings and pointers mitola

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mitola
    New Member
    • Sep 2007
    • 17

    #1

    C strings and pointers mitola

    C program language:

    [code=c]
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <iostream>


    int main()
    {char a[100],b[100],*pa,*pb;
    pa = &a; pb = &b;
    scanf("%s",*pa) ;
    scanf("%s",*pb) ;
    printf("vnesi 2 niza / input 2 strings");
    printf("\nstrin g: %s",*pa);
    printf("string: %s",*pb);
    system("pause") ;
    return 0;
    }
    [/code]

    well i dont know how to connect string and a pointer

    pa = &a; pb = &b; i presume that problem is only here but i cant corect it.
    Last edited by Banfa; Oct 14 '07, 01:09 AM. Reason: Added code tags as per posting guidelines
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by mitola
    C program language:


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



    int main()
    {char a[100],b[100],*pa,*pb;
    pa = &a; pb = &b;
    scanf("%s",*pa) ;
    scanf("%s",*pb) ;
    printf("vnesi 2 niza / input 2 strings");
    printf("\nstrin g: %s",*pa);
    printf("string: %s",*pb);
    system("pause") ;
    return 0;
    }


    well i dont know how to connect string and a pointer

    pa = &a; pb = &b; i presume that problem is only here but i cant corect it.
    Ok, A and B are already pointers so you don't have to get the addresses. If you want a pointer to a char array, you need a pointer to a pointer.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by ilikepython
      Ok, A and B are already pointers so you don't have to get the addresses. If you want a pointer to a char array, you need a pointer to a pointer.
      No on the whole this is rather wrong!

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        This is not wrong
        [code=c]pa = &a; pb = &b;[/code]
        but this would be more conventional
        [code=c]pa = a; pb = b;[/code]
        Generally speaking for an array a as you have defined it the a == &a, the only difference is the type of the expression.

        However you 1st actual problem is in your scanf lines
        [code=c]
        scanf("%s",*pa) ;
        scanf("%s",*pb) ;
        [/code]
        %s tells scanf to expect a pointer to char but you pass a char, *pa these should be
        [code=c]
        scanf("%s",pa);
        scanf("%s",pb);
        [/code]

        And then you make the same mistake in your printf calls
        [code=c]
        printf("\nstrin g: %s",*pa);
        printf("string: %s",*pb);
        [/code]
        should be
        [code=c]
        printf("\nstrin g: %s",pa);
        printf("string: %s",pb);
        [/code]

        However on the whole in this program pa and pb are completely superfluous, you could get rid of them and replace them with a and b.

        Comment

        Working...