Online, an array of strings printf output, and other online ...

Category: C/C++ -> C++ Author: xiaoyunene Date: 2004-09-13 06:46:33
 
xiaoyunene
2004-09-13 06:46:33
I would like to enter a set of array, and then output the compiler no problem, no problem running after the input, the output will be an incorrect report. Trouble you help me look went wrong.

#include <stdio.h>

void prf();

char a[3];

void main()
{
int i;
prf();
char * p;
p=a;
for (i=0;i<3;i++)
{
a[i]=inp(i);
}

for (i=0;i<8;i++)
{
printf("%s",p[i]);
}
}


void prf()
{
printf("input: \n");
}

char inp(int i)
{
scanf("%s",&a[i]);
return a[i];
}

zyyb036
2004-09-13 07:02:41
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h> 
#define MAX 20

void prf();
char* inp(int i);
char *a[3];

void main()
{
    int i;    
    prf();
    char **p;
    p=a;
    for (i=0;i<3;i++)
    {
        a[i]=inp(i);
    }

    for (i=0;i<3;i++)
    {
        printf("%s\n",p[i]);
    }
}


void prf()
{
    printf("input: \n");
}

char* inp(int i)
{
if(!(a[i] = (char*)malloc(sizeof(char)*MAX)))
    exit(-1);
        scanf("%s",a[i]);
getchar();

    return a[i];    
}
pp31562828
2004-09-13 07:22:09

#define MAX_BUF 255
char a[3][MAX_BUF];
scanf("%s",a[0]);
jack92239
2004-09-13 07:59:41
feeling lz code you write good weird
that inp function has no meaning of existence ah. . .
a [i] = inp (i);
inp function is already on a [i] carried out an assignment, you were here once again. . .
lz do not know what the intention is. . .
say the wrong word, as there are two:
first one is the output format, it should be% c (character), not% s (string)
second is:
for (i = 0; i <8; i + +)
{
; printf ("% s", p [i]);
}
i should <3; instead of 8, or cross-border
lizdzhan
2004-09-13 08:30:58

#include <stdio.h>

void prf();
char inp(int i);

char a[3];

int main()
{
    int i;    
    prf();
    char * p;
    p=a;
    for (i=0;i<3;i++)
    {
        a[i]=inp(i);
    }

    for (i=0;i<8;i++)
    {
        printf("%c",p[i]);
    }
    return 0;
}


void prf()
{
    printf("input: \n");
}

char inp(int i)
{
    scanf("%c",&a[i]);
    return a[i];    
}

jj2145531
2004-09-13 08:34:41
# include <stdio.h>

void prf ();
char inp (int i);
char a [3];

void main ()
{
; int i;
prf ();
char * p;
p = a;
for (i = 0; i <3; i + +)
; {
a [i] = inp (i);
}

for (i = 0; i <8; i + +)
{
; printf ("% c", p [i]);}
}

void prf ()
{
printf ("input: \ n");
}

char inp (int i )
{
scanf ("% c", & a [i]); getchar ();
return a [i];
}
a393077080
2004-09-13 08:37:50
"% s" is the output string,% c is a character
wxh12345
2004-09-13 08:55:18
I know change% c no problem, I mean: For example, I want to enter three strings on a [3] inside, then how do I output the string array it
yz05182
2004-09-13 09:10:00
Also, how do you enter three elements, but want to output 8 it? So after some random values ​​are printed out, but in fact these spaces are not initialized, and preferably also changed for (i = 0; i <3; i + +) it

scanf ("% c", & a [i]);
getchar () ;/ / phrase used to absorb the last entered newline, or newline is stored as an array the next element in the
lucifer881105
2004-09-13 09:15:26
want is a string, then you begin to define a wrong
not char a [3];
should be a char * a [3];
or char a [3] [100];
a546386
2004-09-13 09:17:52
Well, reissued under my code

#include <stdio.h>


void prf();
char inp(int i);

char a[3];

void main()
{
int i;
prf();
char * p;
p=a;
for (i=0;i<3;i++)
{
a[i]=inp(i);
}

for (i=0;i<3;i++)
{
printf("%s",p[i]);
}
}


void prf()
{
printf("input: \n");
}

char inp(int i)
{
scanf("%s",&a[i]);
return a[i];
}


I want to achieve the function is to define an array of strings, enter three strings, and outputs them.
amlixi
2004-09-13 09:26:07
solved the problem, according to the 9th floor of ways, thank you! Start posting forget to modify the set points, only 20 points, thank you! Especially abcdef0966
wangkeshuai
2004-09-13 09:37:43
# include <stdio.h>

# define MAX 100 / ** /
void prf ();
char inp (int i);

char * a [3];

void main ()
{
int i;
prf ();
; for (i = 0; i <3; i + +)
{
; a [i] = (char *) malloc (MAX);
}
for ( i = 0; i <3; i + +)
{
inp (i);
}

for (i = 0; i <3; i + +)
{
printf ("% s", a [i]);
}
}

void prf ()
{
printf ("input: \ n");
}

void inp (int i)
{
scanf ("% s", a [i]);
}

shangeaipp
2004-09-13 09:51:59
did not try to compile, if wrong, the landlord and see their changed
onlyenzo
2004-09-13 10:06:57

scanf (" ;% s ", & a [i]) ;/ / you like it or not, a [i] is an array of char type, char can only save one character die. You do not store a string with a char? Do not understand. . .