Help printing out a pointer of strings

Help printing out a pointer of strings

Page 1 of 1

8 Replies - 800 Views - Last Post: 13 August 2009 - 10:13 AM Rate Topic: -----

#1 newbie30  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-June 09

Help printing out a pointer of strings

Post icon  Posted 13 August 2009 - 08:18 AM

Hi;
I have the following code:


int main()
{
		
	  char *array;
	
	int i;
	array = calloc(3, sizeof(char*));
	for(i=0; i<3; i++)
	{
		printf("Enter a string:");
		scanf("%s", &array[i]);
	}

	
	for(i=0;i<3;i++)
	{
		printf("%s\n", array[i]);
	}
		return 0;

}



Problem is the last printf wont print the array of strings, why?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Help printing out a pointer of strings

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Help printing out a pointer of strings

Posted 13 August 2009 - 08:27 AM

%s is expecting the whole string:

#include <cstdio>
#include <cstdlib>
int main() {
	char* arr = (char*)malloc(3*sizeof(char));

	for(int i = 0; i < 3; i++) {
		arr[i] = (char)('A' + i);
	}
	printf("%s", arr);
	free(arr);
	return 0;
}



You don't need a subscript, just send the base pointer.


EDIT, tio print by character:

for(int i = 0; i < 3; i++) {
		printf("%c\n", arr[i]);
	}



Use the code for character rather then an array of characters.

This post has been edited by KYA: 13 August 2009 - 08:29 AM

Was This Post Helpful? 0
  • +
  • -

#3 newbie30  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-June 09

Re: Help printing out a pointer of strings

Posted 13 August 2009 - 08:37 AM

Hi;

So basically if i wanted to read an unlimited number of strings i need to use
char* array[] and first of all either malloc it or calloc it and then i can realloc it if needs be?


Thanks
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,207
  • Joined: 07-September 06

Re: Help printing out a pointer of strings

Posted 13 August 2009 - 08:41 AM

You can go a step easier than that.

All you have to do is define a variable like so:

char* myString;


and then use a cin statement (or scanf) to put the user input into it. It will (at that time) just point to the spot in memory where the string exists.

Hope that make sense.

<edit>
Okay, testing proved to work out a bit differently than I said above. You do need to have a char* myString[] (array), cin (at least) seems to split incoming input at spaces, so an array looks like it is the easiest way of doing things. Sorry about the confusion.
</edit>
Was This Post Helpful? 0
  • +
  • -

#5 newbie30  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-June 09

Re: Help printing out a pointer of strings

Posted 13 August 2009 - 08:55 AM

Hi;

Still confused here, give you my code that i am messing with
				char *array[3];
	int i;

	for(i=0; i<3; i++)
	{
		printf("Enter a string:");
		scanf("%s", &array[i]);
	}

	for(i=0;i<3;i++)
	{
		printf("%s\n", array[i]);
	}




BUt i cant get the strings printed out, any reason why?

Thanks
Was This Post Helpful? 0
  • +
  • -

#6 newbie30  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-June 09

Re: Help printing out a pointer of strings

Posted 13 August 2009 - 09:43 AM

Can some one please answer this for me as very urgent
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5881
  • View blog
  • Posts: 12,758
  • Joined: 16-October 07

Re: Help printing out a pointer of strings

Posted 13 August 2009 - 09:53 AM

int main() {
	// this is a char pointer
	// it will allow you allocate an array of type char.
	// it will NOT allow you to allocate a 2D array of char
	char *array; 
	
	int i;
	
	// you have now allocated memory sized 3*4 (char* most likely being 4 bytes)
	array = calloc(3, sizeof(char*));
	
	// you have NOT allocated an array of strings.  everything from this point in is doomed to failure.




Let's take a step back. You want to dymically allocate an array of strings? But you already know you want 3 of them? Let's go from there.
#include <stdlib.h>
#include <stdio.h>

// size of array
#define ASIZE 3

// max string size
#define STR_SIZE 60

int main() {
	// allocate an array of ASIZE character pointers
	char *array[ASIZE];
	int i;

	for(i=0; i<ASIZE; i++) {
		// allocate memory to our character pointer
		array[i] = malloc(STR_SIZE);
		printf("Enter a string:");
		scanf("%s", array[i]);
	}

	
	for(i=0;i<ASIZE;i++) {
		printf("%s\n", array[i]);
	}
	return 0;
}



I know I probably just did the homework, put it's real important that you get this or the rest of C will kick you butt. Please read it over and understand it.
Was This Post Helpful? 0
  • +
  • -

#8 newbie30  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-June 09

Re: Help printing out a pointer of strings

Posted 13 August 2009 - 10:01 AM

Thanks and its not a homework thing, jsut something i am working on my own.
Was This Post Helpful? 0
  • +
  • -

#9 newbie30  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-June 09

Re: Help printing out a pointer of strings

Posted 13 August 2009 - 10:13 AM

Just another quick question. If i needed to add more on to the array, i take it i just use the realloc function like this:

*array = realloc(array, 7*sizeof(STR_SIZE);




Also would i then need to malloc the other new 4 segments created for the array?

Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1