Im trying to understand why a simple function I wrote doesnt work as it should, its probably some simple noob mistake, but its really making no sense.
Here it is:

void justapoe(char seq[], int pos){
	char c;
	int i;
	
	printf("Insert: ");
	scanf("%c", &c);
	
	seq[pos]=c;
	printf("Seq: ");
	for(i=10;i<50;i++){
		printf("%c", seq[i]);
	}
	printf("\n");
}

Here is the output:
Insert: Seq:
@x&
@<


Or some similar nonsense.

Recommended Answers

All 9 Replies

for(i=10;i<50;i++) This will display every character starting at seq[10] and will stop at seq[49].
Is the array that you passed as an argument in justapoe() that long? like char seq[50]. Has it been initialized with some value already? Before passing it to the function?, if not, guess what?
It contains only garbage. seq[pos]=c; What's seq[pos], is it seq[0]?. Why assigning only one char to one of the members of the array?

For the sake of being helpful:

/* justapoe.c */

#include <stdio.h>

/* function to pass a string and to change some
 * characters inside according to the position
 * given
 */
void justapoe( char seq[], int pos )
{
    int i = 0; /* for looping */

    /* changing just one char of the string */
    seq[pos] = '6'; /* this can be done via input request */

    /* display one character at a time */
    for ( i = 0; i < 5; i++ ) /* can't loop more than 5 times */
    {                         /* because that's how long the */
        putchar( seq[i] );    /* string is, don't go beyond */
    }
    putchar( '\n' ); /* give me a new line */
}

int main ( void )
{
    char string[] = "12345";
    int position = 2;

    /* display string without modification */
    printf( "Original string = %s\n", string );

    /* display the result of justapoe */
    printf( "String modify by function = " );

    /* call to the function justapoe */
    justapoe( string, position ); /* Let's change the 2 */

    printf( "Here's again = " );
    /* Another way of displaying the same string without
     * the need of a loop */
    printf( string );

    return 0;
}

Thanks for your reply.
First off seq is indeed initialized as seq[50]. Than look at the output, the program doesnt even wait for me to enter a char after the "Insert: " statement. Thats really odd! Than the variable pos represents the place in the string where the char is supposed to go. It is initialized to 10 and it is incremented after I call the function. I only place one char to each member of the array because I want to remove a single char easely when needed. Can I be calling the function in a wrong way? I would post the main function but it is a bit long, but if it is needed I will post it. Thanks for your help.

>First off seq is indeed initialized as seq[50].
seq[50] that's declaring a variable; initialized means you assign value to it.

char seq[50]; /* seq is declared as an array of 50 chars */
char seq[50] = "This is a string"; /* seq is being initialized with value */

If you don't initialize an array of char it will contain random data that will mean nothing to you.


>the program doesnt even wait for me to enter a char after the "Insert: " statement.
That's because when you use scanf() to read the character, you are entering a key plus the ENTER key, which stays in the buffer waiting to next time that scanf() is called.
It doesn't wait, because is reading already a char, and it is all happy.

>Can I be calling the function in a wrong way?
Don't know. Are you calling it as I show to you in my example above? If you are, then you are doing it right.

>seq[50] that's declaring a variable; initialized means you assign value to it.
Yes, I know that, it was just very late where I am.
Ok, now I understand better the virtues of initializing an array.
What I still dont understand very well is the scanf() part. Is there something in buffer which the scanf gets?
Like something you would correct with fflush()? Thats so weird, its just a simple scanf...

you cant "fflush" an input buffer.

the best thing to do is not use "scanf", but instead use "gets", and parse the entire string for everything that's entered, whether it's valid or garbage or a mixture of both.

you cant "fflush" an input buffer.

the best thing to do is not use "scanf", but instead use "gets", and parse the entire string for everything that's entered, whether it's valid or garbage or a mixture of both.

Do you want me to force you to wash your mouth with soap?
What the heck are you thinking? gets() is not an option. Forget that even exist.
There's no possible way of limiting the amount of input from stdin.
I am going to give you the benefit of the doubt and think that you meant fgets(). That's the proper function to use.

commented: your absolutely right. +3

DOH!!

i knew that... really i did.

yes, you're right... never use gets().. always use fgets().

im sorry, that was a typo/accident. i didnt bother to proofread it for some reason.

LightSystem adiciona-me no MSN, preciso de falar contigo e preciso de uma ajuda.. <email removed>

engrish, por favor.

and dont post your email address

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.