5 Replies - 620 Views - Last Post: 30 January 2010 - 06:17 PM Rate Topic: -----

#1 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 215
  • View blog
  • Posts: 586
  • Joined: 04-August 09

Array of strings

Posted 30 January 2010 - 02:37 PM

I'm trying to print words in the reverse order they were entered eg.

input: hello there
output: there hello

but i get a segmentation fault after the input

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

int main()
{
	int i,num;
	char *arr[10];

	printf("Enter number of words :");
	scanf("%d",&num);

	for(i=0;i<num;i++)
	{
		scanf("%s",arr[i]);
	}

	for(i=num-1;i>=0;i--)
	{
		printf("%s ",arr[i]);
	}
		return 0;
}





This post has been edited by simeesta: 30 January 2010 - 03:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Array of strings

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Array of strings

Posted 30 January 2010 - 03:56 PM

The problem is that you have allocated memory for the 10 pointers, but no space to store the actual strings.

Instead of
char *arr[10];
you should use something like
char arr[10][20];
to allocate space for 10 strings of 20 characters each (well, actually 19 characters plus a null char '\0' to terminate each string).
Was This Post Helpful? 0
  • +
  • -

#3 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: Array of strings

Posted 30 January 2010 - 04:10 PM

And you can dynamically allocate the memory. Like:
#include <stdio.h>
#include <stdlib.h>

#define MAX_STRING_SIZE 256

int main()
{
	int i,num;
	char **arr;

	printf("Enter number of words :");
	scanf("%d",&num);

	arr = (char**) malloc(num * sizeof(char*));
	
	for(i=0;i<num;i++)
	{
		arr[i] = (char*) malloc(MAX_STRING_SIZE * sizeof(char));
		scanf("%s",arr[i]);
	}

	for(i=num-1;i>=0;i--)
	{
		printf("%s ",arr[i]);
	free(arr[i]);
	}
	
	free(arr);
	
		return 0;
}

Was This Post Helpful? 1
  • +
  • -

#4 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 215
  • View blog
  • Posts: 586
  • Joined: 04-August 09

Re: Array of strings

Posted 30 January 2010 - 05:44 PM

View Postanonymouscodder, on 30 Jan, 2010 - 03:10 PM, said:

And you can dynamically allocate the memory. Like:
#include <stdio.h>
#include <stdlib.h>

#define MAX_STRING_SIZE 256

int main()
{
	int i,num;
	char **arr;

	printf("Enter number of words :");
	scanf("%d",&num);

	arr = (char**) malloc(num * sizeof(char*));
	
	for(i=0;i<num;i++)
	{
		arr[i] = (char*) malloc(MAX_STRING_SIZE * sizeof(char));
		scanf("%s",arr[i]);
	}

	for(i=num-1;i>=0;i--)
	{
		printf("%s ",arr[i]);
	free(arr[i]);
	}
	
	free(arr);
	
		return 0;
}




thank you this solved the problem, but could you explain what the following code does? whats the difference between the first and second memory allocation. thank you

arr = (char**) malloc(num * sizeof(char*));

for(i=0;i<num;i++)
	{
		arr[i] = (char*) malloc(MAX_STRING_SIZE * sizeof(char));


This post has been edited by simeesta: 30 January 2010 - 05:48 PM

Was This Post Helpful? 0
  • +
  • -

#5 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: Array of strings

Posted 30 January 2010 - 06:10 PM

What is an array? Is a list of variables right? So, for example, an array of 5 int occupies the same memory of 5 int variables.

And do you know the sizeof operator? The sizeof is used to calculate the size of data types. So, sizeof(int) will return how much memory an int occupies in your environment.

arr = (char**) malloc(num * sizeof(char*));


num represents the array length. So if it is an array of char* num * sizeof(char*) will be the size that the array of char* of the length num occupies.

But what about the (char**)? It is a casting (type conversion). Why is that necessary? Because the malloc function returns a generic pointer, void*. So this way we optimized saying that we need what we allocated as char**. I personally use cast even when it's not required. Sometimes an implicit cast is made for you, but I don't like this kind of thing (I like to explicit my ideas).

Now we allocated our array of strings (char*). But we have to allocate each string of the array. That's the purpose of:
arr[i] = (char*) malloc(MAX_STRING_SIZE * sizeof(char));


It allocates MAX_STRING_SIZE characters for each string.

The calls to the free() function just deallocates the memory that we allocated before. As I said, I like to do everything explicit.

Please, if you still have questions post here and specifies exactly which part you have don't understand.
Was This Post Helpful? 1
  • +
  • -

#6 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 215
  • View blog
  • Posts: 586
  • Joined: 04-August 09

Re: Array of strings

Posted 30 January 2010 - 06:17 PM

Thanks for the help. It's appreciated.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1