C code is not generating required results.

V

vasudevmukherjee

Hi! Can somebody help tell me why the following code gives a garbage
value while producing first student's name, whereas it gives the names
correctly for other three students - I really fail to understand since
it is not generating the required results.- Thank you in anticipation -
Vasudev.

#include <stdio.h>
main ()
{
char student[4][30], *x[4][30];
int i, j,k;
printf("Student's name!\n");
for(i=0;i<=3;i++)
{
gets(student);
x[30]=&student[30];
}
printf("\nYou have typed the following names.\n");
for(i=0;i<=3;i++)
{
puts(*x);
}
printf("\nHow many names do you want to republish (choice 1 to
4)??\n");
scanf("%d",&k);
k=k-1;
if(k>=0)
{
for(j=0;j<=k;j++)
{
printf("Which Student's name do you want to know, 1st, 2nd, 3rd or
4th??\n");
scanf("%d", &i);
i=i-1;
printf("Student's name = %s\n", *x);
}
}
}
 
M

mailursubbu

Try the below one... It is working now.... You were not populating the
x array properly...

#include <stdio.h>
main ()
{
char student[4][30], *x[4][30];
int i, j,k;
printf("Student's name!\n");
for(i=0;i<=3;i++)
{
//gets(student);
scanf("%s",student);
printf("%s", student);
*x=student;


}


printf("\nYou have typed the following names.\n");
for(i=0;i<=3;i++)
{
puts(*x);

}


printf("\nHow many names do you want to republish (choice 1 to
4)??\n");
scanf("%d",&k);
k=k-1;
if(k>=0)
{
for(j=0;j<=k;j++)
{
printf("Which Student's name do you want to know, 1st, 2nd, 3rd or
4th??\n");
scanf("%d", &i);
i=i-1;
printf("Student's name = %s\n", *x);



}
}
}
 
V

vasudevmukherjee

Hi Dear!

Thank you very much for the trouble you took to respond my mail. I
tried your code but unfortunately this is also giving error. You know,
when entering names normaly we have first and last name having a space
in between, that's why I used 'gets' which was assigning the names to
pointer very well except the first name. Now in your code I find
'//gets(). I don't know what these double slashes are for and why you
used scanf and printf statements after gets(). However, this code is
not accepting spaces in between the first / last names and invariably
giving error.

However, thanking you once again for your help but if you could respond
my above queries I'll be grateful to you.

Vasudev
 
C

Christopher Benson-Manica

#include <stdio.h>
main ()

int main( void )
{
char student[4][30], *x[4][30];
int i, j,k;
printf("Student's name!\n");
for(i=0;i<=3;i++)
{
gets(student);


NEVER use gets(). Ever. Your alternatives include fgets() and scanf().
x[30]=&student[30];


You're overindexing. x[3][30] does not exist. What exactly is your x
array accomplishing for you? (Hint: Not much.)

I haven't looked to see where your real error might be. In the
future, fix your code indentation. It makes offering help easier.
 
M

Mark McIntyre

On 14 Dec 2005 03:20:14 -0800, in comp.lang.c ,
#include <stdio.h>
main ()
{
char student[4][30], *x[4][30];
int i, j,k;
printf("Student's name!\n");
for(i=0;i<=3;i++)
{
gets(student);


DONT use gets - see the FAQ for why.
x[30]=&student[30];


In C, arrays start from zero, so student goes from 0 to 29. The
code points the 31st element of x to the 31st element of
student. Neither of these arrays has 31 elements, so you just
trashed your applications memory space.


Also, why are you doing this? Perhaps you wanted to point x to
student ? Or copy the value? This doesn't do either of these
things. I suggest you explain what you want to do here.
puts(*x);


At this point, *x points to nowhere, so you may well get a
programme crash. I did.
Any subsequent output will be just luck.
scanf("%d",&k);

Don't use scanf - see the FAQ.
 
R

Richard Heathfield

(e-mail address removed) said:
//gets(student);
scanf("%s",student);


The // comment syntax, which was introduced in C99, is not supported by C as
currently implemented. (C99 does not appear to have been implemented yet.)

Well done for removing the buffer overflow vulnerability, though. What a
shame that you replaced it with a buffer overflow vulnerability.
 
R

Richard Bos

Richard Heathfield said:
(e-mail address removed) said:
//gets(student);
scanf("%s",student);


The // comment syntax, which was introduced in C99, is not supported by C as
currently implemented. (C99 does not appear to have been implemented yet.)


That's rather over-stated. It has not been _commonly_ or _completely_
implemented. Then again, I know of no complete implementation of ISO
C++, either, and people use that.

Richard
 
R

Richard Heathfield

Richard Bos said:
Richard Heathfield said:
(e-mail address removed) said:
//gets(student);
scanf("%s",student);


The // comment syntax, which was introduced in C99, is not supported by C
as currently implemented. (C99 does not appear to have been implemented
yet.)


That's rather over-stated. It has not been _commonly_ or _completely_
implemented.


Well, what I actually said was that it does not *appear* to have been
implemented. This is certainly true from my perspective, since I know of no
conforming C99 implementations - so none have appeared, as far as I am
concerned.

Since any C90 implementation can be viewed as a not-completely-implemented
C99 implementation, there's little point in having a
"not-completely-implemented" category. As for "commonly", until it *is*
commonly (and completely) implemented, there is little point in using it
from a comp.lang.c perspective, where portability is rightly held in high
regard.
Then again, I know of no complete implementation of ISO C++, either,
Indeed.

and people use that.

Why? :)
 
K

Keith Thompson

Richard Heathfield said:
Since any C90 implementation can be viewed as a not-completely-implemented
C99 implementation, there's little point in having a
"not-completely-implemented" category.

I'd say that one reasonable measure of partial C99 compatibility is
how much of C99 that's not part of C90 is implemented. A conforming
C99 implementation would be 100%; a conforming C90 implementation that
implements no C99 features would be 0%. (Assigning percentages to
intermediate states isn't necessarily meaningful.)
 
V

vasudevmukherjee

Hi Christopher!

I know that in C counting starts from 0 and when I write x[4] that
means x[0] to x[3]. Now my problem is that when I put the following
statement:
printf("Student's name!\n");

for(i=0;i<=3;i++)
{
gets(student);
x[30]=&student[30];
}

the x[30] should save the address of student[30] for future
reference. Now this '' can be 0 to 3, so that whenever next in the
program I refer to *x[30] it should refer to the address to the
respective '' whether it is 0, 1, 2 or 3 and from my program you can
see that I have not gone beyond 4 names. Same is the case with [30]
this also refers to 0 through 29 (since I have marked "i=i-1" or
"k=k-1" in the program).

Plesae note it gives problem for 1st name only, but all other names are
coming absolutely correctly that means its not saving the name at
'i=0', why? I am confused a bit and need help.

However, I'll keep in mind your suggestion of indentation.

Vasudev
#include <stdio.h>
main ()

int main( void )
{
char student[4][30], *x[4][30];
int i, j,k;
printf("Student's name!\n");
for(i=0;i<=3;i++)
{
gets(student);


NEVER use gets(). Ever. Your alternatives include fgets() and scanf().
x[30]=&student[30];


You're overindexing. x[3][30] does not exist. What exactly is your x
array accomplishing for you? (Hint: Not much.)

I haven't looked to see where your real error might be. In the
future, fix your code indentation. It makes offering help easier.
 
P

Peter Shaggy Haywood

Groovy hepcat (e-mail address removed) was jivin' on 14 Dec 2005
03:20:14 -0800 in comp.lang.c.
C code is not generating required results.'s a cool scene! Dig it!
Hi! Can somebody help tell me why the following code gives a garbage
value while producing first student's name, whereas it gives the names
correctly for other three students - I really fail to understand since
it is not generating the required results.- Thank you in anticipation -
Vasudev.

Your code is extremely hard to read. This is due to the total lack
of indentation and other white space. You also have line wrapped
string literals. It's a mess! So, I have reformatted it. Now we can
begin to identify things that need attention.
#include <stdio.h>

main ()

Make that

int main(void)
{
char student[4][30], *x[4][30];
int i, j, k;

printf("Student's name!\n");

for(i = 0; i <= 3; i++)

Don't use "magic numbers". You should use the actual number of
elements in the array. How do you do that? Simple! Divide the size of
the array by the size of an element. Or, to put it another way:

for(i = 0; i < (sizeof student / sizeof *student); i++)
{
gets(student);


As other respondants have already told you, never use gets(). It
bears repeating. Never use gets(). Why? Simple! There is no way to
prevent the user of your program from entering more characters than
the array you've provided can hold. You have enough space for 29
characters (plus the terminating '\0'). Now, what if the user, either
through malice or sheer idiocy, enters 30 or more? It will overflow
your array, causing undefined behaviour; that's what.
So never use gets(). So what do you do instead? Again, simple! You
use fgets(). Look that one up in your C book. But first, read the FAQ
(http://www.eskimo.com/~scs/C-faq/top.html)
x[30] = &student[30];


What on Earth is the point of that? There's absolutely no need. You
can use student everywhere you use *x below. So you just don't
need x at all.
}

printf("\nYou have typed the following names.\n");

for(i = 0; i <= 3; i++)
{
puts(*x);
}

printf("\nHow many names do you want to republish (choice 1 to 4)??\n");
scanf("%d", &k);


When you read the FAQ you will see that scanf() is not the best
function to use for this and what to do instead.
k = k - 1;

What's the point of that? The usual form of a for loop is for(X = 0;
X < Y; X++) where X is the loop index and Y is the number of times you
want to loop. So, in this case, you want k to be the number of times
you want to loop, not one less than that number.
if(k >= 0)

What about the possibility of k being greater than 3 (or 4 if you do
the loop the usual way as shown above)? A better approach would be to
write a function to get a number and make sure it is within range.
(See my code below.)
{
for(j = 0; j <= k; j++)

You were using i for your loop index before. Why change it now? Be
consistent. This is the sort of thing that can make code harder to
understand. Think about the next person who has to read your code
(whether it be yourself or someone else), and do what he/she most
expects.
{
printf("Which Student's name do you want to know, 1st, 2nd, 3rd or 4th??\n");
scanf("%d", &i);
i = i - 1;

Or, in other words:

i--;
printf("Student's name = %s\n", *x);
}
}


Return something. Portable return values for main() are 0,
EXIT_SUCCESS and EXIT_FAILURE (the latter two being macros defined in
stdlib.h).

return 0;

Here's an improved version of your code using extra functions to get
input.

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

#define ARRAY_SIZE(a) (sizeof a / sizeof *a)

/* Read in a line of text from stdin. Read only up to
enough characters to fit into buf, discarding the
rest of the line if more characters remain. Remove
newline from the input. */
void get_line(char *buf, size_t buflen)
{
char *p;

if(fgets(buf, buflen, stdin))
{
/* Find the newline (which fgets() retains)... */
p = strchr(buf, '\n');
if(p)
{
/* ...and remove it. */
*p = '\0';
}
else
{
/* Newline not found: more input remains in stdin. Remove it. */
int c;

while(EOF != (c = getchar()) && '\n' != c)
;
}
}
else
{
fputs("\nError reading from stdin.\n", stderr);
exit(EXIT_FAILURE);
}
}

/* Display a prompt and read in a number within a certain range.
Keep trying until a number (and ONLY a number) is read in
and it is within the given range. */
int get_num_in_range(const char *prompt, int low, int high)
{
char buf[20];
int n;
char c;

if(low > high)
{
/* Swap 'em. */
n = low;
low = high;
high = n;
}

do
{
/* Display the prompt. */
printf("%s (%d to %d): ", prompt, low, high);
fflush(stdout);

/* Get the input. */
get_line(buf, sizeof buf);

/* Extract the number. Loop if not (only) a number
or if number not in range. */
}while(1 != sscanf(buf, "%d%c", &n, &c) || n < low || n > high);

return n;
}

int main(void)
{
char student[4][30];
int i, j, k;

printf("Student's name:\n");
fflush(stdout);

for(i = 0; i < ARRAY_SIZE(student); i++)
{
/* Get the name. */
get_line(student, sizeof student);
}

printf("\nYou have typed the following names.\n");

for(i = 0; i < ARRAY_SIZE(student); i++)
{
puts(student);
}

k = get_num_in_range("\nHow many names do you want to republish?",
1, ARRAY_SIZE(student));

for(i = 0; i < k; i++)
{
j = get_num_in_range("Which Student's name do you want to know?",
1, ARRAY_SIZE(student));
j--;

printf("Student's name = %s\n", student[j]);
}

return 0;
}

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top