Jump to content
Can't remember your login details? Read more... ×
Sign in to follow this  
Shikimaru

Help Please

Recommended Posts

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

void test(char pong[20])
{
	printf("Enter pong:");
	scanf("%s", &pong);
	printf("Pong = %s", pong);
}

int main(void)
{
	char pong[20];

	test(pong);



return (0);
}

I keep getting error's with this code, please help me out!

Share this post


Link to post
Share on other sites

When i try and recall the inputted value in

 

printf("Pong = %s", pong);

The program stops and puts up an error message (dialog box) with"Segment Violation" Written in it

Share this post


Link to post
Share on other sites

When i try and recall the inputted value in

 

printf("Pong = %s", pong);

The program stops and puts up an error message (dialog box) with"Segment Violation" Written in it

Are you sure it is not stopping just before there?

Share this post


Link to post
Share on other sites

It prints

 

"Pong = " and then stops

Ah ok. I guess it didn't show up until later. Ok, basically your function isn't doing what it should be doing. Specifically pong inside the function test is a char (*)[20], that is a pointer to an array of 20 chars. So for the scanf, you don't want to pass in &pong, which is char **, that is the address of the pointer pong. You want to just put pong there. That means your pong array is uninitialised and possibly without a '\0' in there, so when you printf it will start walking over areas of the memory that your program doesn't have access to, and cause the segmentation fault.

 

If you compiled with warning on, your compiler will warn you about this. That is if your compiler doesn't suck. If it is gcc, use -Wall at a minimum.

 

Oh and if are wondering why char pong[20] doesn't give you an array, thats because in C char * p, char p [], and char p[20] all give you a char *. The 20 is ignored.

Share this post


Link to post
Share on other sites

A good way to think of this when you're learning it is that the name of an array is just a pointer to the first element of the array.

 

Strictly speaking they're not the same though, arrays are actually const pointers so they're not dynamic (you can't increment them like pointers for example). If you're familiar with const this may help:

/* These two declerations are equivalent */
char string[] = "hello";
char *const string = "hello";
Also to clarify, the [20] isn't always ignored. When declaring new variables it allocates space for the array on the stack, and when you have multi-dimensional arrays it lets the compiler figure out where each dimension ends and the next begins. But yeh, for a single dimensioned function parameter it can be fairly redundant to the compiler; although not necesarily to the programmer :)

Share this post


Link to post
Share on other sites

#include <stdio.h>

void test(char* pong)
{
  printf("Enter pong:");
  // Make sure you give a size (length - 1 to allow for a NULL terminator)
  scanf("%19s", pong);
  printf("Pong = %s\n", pong);
}

int main(int argc, char** argv)
{
  char pong[20];

  test(pong);
  printf("Pong again: %s\n", pong);

  return 0;
}
The above compiles and and works correctly with gcc (using -Wall). I've also added another printf statement to show that your original char array has been assigned the value. Also note that a size has been used on scanf, this is most important to prevent your application being exploited via buffer overflow attacks, even if it's not likely it's a good habit to get yourself into.

 

It's not entirely how I would do this, you are assuming function test is being passed a char array of a particular length but it does demonstrate a working example.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×