Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

char arrays in C

Name: Anonymous 2012-09-12 20:46

So, I'm trying to learn C, and currently I'm testing out characters and character arrays. I've put together this code:


#include <stdio.h>
int main()
{
    char hello[7];
    int a, b;
   
    puts("Enter to start:\n");
    getchar();
    fflush(stdin);
   
    puts("Intiated:");

    for(a = 0; a <= 7; ++a){
          hello[a] = getchar();
      }
      fflush(stdin);
     
      for(a = 0; a <= 7; ++a){
            putchar(hello[a]);
            }
           
            fflush(stdin);
            getchar();
            return(0);
}


And it works well (or as well as it is suppose to), but if I change the char array hello's size to something smaller than 7, or change the 7 in both the 'for' parts, than I'll capture more than the number the char array was set to.

So, for example:


#include <stdio.h>
int main()
{
    char hello[3];
    int a, b;
   
    puts("Enter to start:\n");
    getchar();
    fflush(stdin);
   
    puts("Intiated:");

    for(a = 0; a <= 9; ++a){
          hello[a] = getchar();
      }
      fflush(stdin);
     
      for(a = 0; a <= 9; ++a){
            putchar(hello[a]);
            }
           
            fflush(stdin);
            getchar();
            return(0);
}


This will capture 9 characters, even though 'hello' is set to 3. Frankly, I don't mind this. It's better than an overflow or some other error. But what exactly happens? Does it just change the amount of characters the array can hold?

Name: twkm.freeshell.org/c.html 2012-09-12 20:56

>>1
kid, you are gonna get raped.

Is it not /prog/help/, this is dis[/cussion]/4chan.org/[about]prog[/ramming]

Go get a teacher/professor to help you with your code homework.

Also, there are other boards, and IRC channels for this. Do not spam this thread with jimmy code.

Name: Anonymous 2012-09-12 21:07

>>2
Although I'd hate to promote cancer on 4chan, I must retort to what I believe to be an ill-found response to my post:

I understand this isn't a help board. But, as you said, this is a discussion board. And I am trying to discuss the dynamics of a piece of code.

As I clearly pointed out, there is nothing wrong with this code. In fact, there is nothing to be wrong with this code. It is something I put together quickly to try and understand characters in the C language. And in doing so, I learned much. But I still had more I wanted to learn, and the only way for me to do this is to discuss it.

This is not homework. This is a hobby.

And, if I may be so bold, this board isn't filled to the brim with awe inspiring discussion. At least my post actually discusses programming. And who knows, maybe through a discussion that could have been had because of my post, you could have learned something as well.

Name: Anonymous 2012-09-12 21:26


Discarding the return value of getchar.
Not comparing the return value of getchar with EOF and handling the case usefully.
Passing an input stream to fflush.

You are a bad man.

Name: Anonymous 2012-09-12 21:32

>>4
Help me understand what I could do to correct my code. Like I said, I only typed this up to test characters and character arrays.

Name: Lambda Arthur Calculus 2012-09-12 21:35

Hi!! :D

#include <stdio.h>

int main(void)
{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int n = 7, c;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while ((c = getchar()) != EOF && n--)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;putchar(c);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;
}

Name: Lambda Arthur Calculus 2012-09-12 21:39

Bye!! :D

#include <stdio.h>

static int readdatlinegrandma(char *lambda, size_t calculus)
{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int arthur;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;calculus--;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while ((arthur = getchar()) != EOF && arthur != '\n')
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (calculus) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*lambda++ = arthur;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;calculus--;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*lambda = '\0';
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return arthur;
}

int main(void)
{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char hams[7];

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts("INITIATED!!!!!!!");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (readdatlinegrandma(hams, sizeof hams) != EOF)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts(hams);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;
}

Name: Lambda Arthur Calculus 2012-09-12 21:40

looooooooool

Name: Lamda Arthur Calculus 2012-09-12 21:40

C source code gotta be HTML5 compliant.

Name: Lambda Arthur Calculus 2012-09-12 21:41

Das cuz I got 20 years experience with HTML5 technologies

Name: Anonymous 2012-09-12 21:49

I don't know what Lambda is pullin here, but thanks for the code in >>6

Name: Anonymous 2012-09-12 23:20

>>6
#include <stdio.h>

int main(void)
{
        int n = 7, c;

        while ((c = getchar()) != EOF && n--)
                putchar(c);
        return ferror(1) ? 0 : 1;    /* ENTERPRISE */
}

Name: Anonymous 2012-09-13 3:34

>>12
COME ON
#include <stdio.h>

int main(void)
{
        int n = 7, c;

        while ((c = getchar()) != EOF && n--)
                putchar(c);
        return ferror(stdin) ? EXIT_FAILURE : EXIT_SUCCESS;
}

Name: Anonymous 2012-09-13 3:45

>>1
1. You should not store getchar() into your char array directly. Store it into an int first in order to check the return value properly. For the reason behind this, read the documentation.

2. Flushing stdin is undefined behaviour by the ANSI standard.

3. As for writing to hello[6] when you have hello defined with length 3... just cause you ran it without it crashing on your computer does not mean it ``works''. You're just going out of bounds which is undefined behaviour. Who knows what is in memory after that array, and you may have just overwrote it. It does not automatically make hello bigger - you've defined it with length 3 and that's fixed. If you need to capture an arbitrary number of chars use a dynamic array you resize up everytime it fills to whatever limit.

IHBT

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2012-09-13 5:16

The very first thing you have to recognise is the finiteness of memory.

Much like you don't try parking your car in a space half its length, don't go past the allocated length of the array.

Idiots like these can't do basic grade school maths and blame the language for the problems their stupidity created.

Name: Anonymous 2012-09-13 23:43

>>13
You forgot your <stdlib.h>.

Also, YHBT.

Don't change these.
Name: Email:
Entire Thread Thread List