By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,379 Members | 1,602 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,379 IT Pros & Developers. It's quick & easy.

C code is not generating required results.

P: n/a
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[i]);
x[i][30]=&student[i][30];
}
printf("\nYou have typed the following names.\n");
for(i=0;i<=3;i++)
{
puts(*x[i]);
}
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[i]);
}
}
}

Dec 14 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
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[i]);
scanf("%s",student[i]);
printf("%s", student[i]);
*x[i]=student[i];
}
printf("\nYou have typed the following names.\n");
for(i=0;i<=3;i++)
{
puts(*x[i]);

}
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[i]);

}
}
}

Dec 14 '05 #2

P: n/a
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

ma*********@gmail.com wrote:
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[i]);
scanf("%s",student[i]);
printf("%s", student[i]);
*x[i]=student[i];
}
printf("\nYou have typed the following names.\n");
for(i=0;i<=3;i++)
{
puts(*x[i]);

}
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[i]);

}
}
}


Dec 14 '05 #3

P: n/a
va**************@yahoo.co.uk wrote:
#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[i]);
NEVER use gets(). Ever. Your alternatives include fgets() and scanf().
x[i][30]=&student[i][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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dec 14 '05 #4

P: n/a
On 14 Dec 2005 03:20:14 -0800, in comp.lang.c ,
va**************@yahoo.co.uk wrote:
#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[i]);
DONT use gets - see the FAQ for why.
x[i][30]=&student[i][30];
In C, arrays start from zero, so student[i] goes from 0 to 29. The
code points the 31st element of x[i] to the 31st element of
student[i]. 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[i] to
student[i] ? Or copy the value? This doesn't do either of these
things. I suggest you explain what you want to do here.
puts(*x[i]);
At this point, *x[i] 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.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 14 '05 #5

P: n/a
ma*********@gmail.com said:
//gets(student[i]);
scanf("%s",student[i]);


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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 14 '05 #6

P: n/a
Richard Heathfield <in*****@invalid.invalid> wrote:
ma*********@gmail.com said:
//gets(student[i]);
scanf("%s",student[i]);


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
Dec 15 '05 #7

P: n/a
Richard Bos said:
Richard Heathfield <in*****@invalid.invalid> wrote:
ma*********@gmail.com said:
> //gets(student[i]);
> scanf("%s",student[i]);
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? :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 15 '05 #8

P: n/a
Richard Heathfield <in*****@invalid.invalid> wrote:
Richard Bos said:
Then again, I know of no complete implementation of ISO C++, either,


Indeed.
and people use that.


Why? :-)


Ah, now, _there_ you have me beat.

Richard
Dec 15 '05 #9

P: n/a
Richard Bos wrote:
Richard Heathfield <in*****@invalid.invalid> wrote:
Richard Bos said:
Then again, I know of no complete implementation of ISO C++,
either,


Indeed.
and people use that.


Why? :-)


Ah, now, there you have me beat.

Everybody else was doing it, I just wanted to be popular.

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Dec 15 '05 #10

P: n/a
Richard Heathfield <in*****@invalid.invalid> writes:
[...]
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 15 '05 #11

P: n/a
"Default User" <de***********@yahoo.com> wrote:
Richard Bos wrote:
Richard Heathfield <in*****@invalid.invalid> wrote:
Richard Bos said:

> Then again, I know of no complete implementation of ISO C++,
> either,

Indeed.

> and people use that.

Why? :-)


Ah, now, there you have me beat.


Everybody else was doing it, I just wanted to be popular.


Fairy nuff, but does that refer to using C++ or to beating me?

Richard
Dec 16 '05 #12

P: n/a
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[i]);
x[i][30]=&student[i][30];
}

the x[i][30] should save the address of student[i][30] for future
reference. Now this '[i]' can be 0 to 3, so that whenever next in the
program I refer to *x[i][30] it should refer to the address to the
respective '[i]' 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

Christopher Benson-Manica wrote:
va**************@yahoo.co.uk wrote:
#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[i]);


NEVER use gets(). Ever. Your alternatives include fgets() and scanf().
x[i][30]=&student[i][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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.


Dec 16 '05 #13

P: n/a
Groovy hepcat va**************@yahoo.co.uk 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[i]);
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[i][30] = &student[i][30];
What on Earth is the point of that? There's absolutely no need. You
can use student[i] everywhere you use *x[i] 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[i]);
}

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[i]);
}
}
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[i], sizeof student[i]);
}

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

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

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"?
Dec 18 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.