Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Cheer

macrumors newbie
Original poster
Mar 18, 2008
5
0
Hello, could anyone help me with C language a little. I'm trying to write a program with pointers, which would scan some words to the array of strings and would print them on a screen. I'm new to C programming, that's why it's so difficult to deal with such a simple task :confused:
Code:
#include <stdio.h> 
#include <string.h>  
  int main () {    
               char *  string[3];   
              int i;  
              
               ..............    
          
             return (0);  }
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
To print use printf, in Terminal type man printf, for more information. To read in, use scanf or ideally fgets type man scanf or man fgets for more information.
 

Cheer

macrumors newbie
Original poster
Mar 18, 2008
5
0
I was trying to. But there are still some mistakes which I can't find.. :(

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

int main ()
{
	char* string[3];
	int j;
	for(j=0;j<3;j++)
	{
	
	fgets(string[j], sizeof string[j], stdin);
		
	}

	for(j=0;j<3;j++)
	{
		printf("String number %d: \"%s\"\n", j, string[j]);
	}
	return 0;
}
 

mheidt

macrumors member
Jun 7, 2005
94
0
If you face a problem like this you need to get hold of the values by debugging, tracing etc.

F.e. what do you expect the value of
sizeof string[j]
to be?
 

pilotError

macrumors 68020
Apr 12, 2006
2,237
4
Long Island
A pointer only points to a set of characters that were allocated. Since you didn't initialize the pointers to NULL, you really have no idea what they are pointing to.

You created 3 character pointers, but you haven't allocated any memory for them to point to.

You can do a couple of things, create a variable (maybe an array of n characters by 3 - a matrix if you will) that you can point the pointer to, or you can allocated memory by calling malloc within your for loop before the fgets call.

mheidt kind of pointed you in that direction.
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
This code, as pilotError mentions, allocates an array of three pointers:
Code:
char* string[3] ;

and it is not an array of strings.

The array will be 12 bytes long; 3 entries of 4 bytes each; each pointer being 4 bytes long.

Since you don't initialize them, they will have random data in them. Since you declare these values as pointers, (via char *), your program will expect them to contain pointers.

However, when you read data via fgets(), you are reading from stdin directly into the storage pointed to by string[j], your pointer field, which, is not pointing to anything meaningful.

You could / should read into dynamically obtained storage, and put the address of that dynamic memory into your string array entry.

Todd
 

iSee

macrumors 68040
Oct 25, 2004
3,540
272
try declaring your "string" variable like this:

char string[3][100];

This way, string is a pointer to three buffers of 100 characters each.
As others pointed out, your current way declares an array of three pointers to characters, but the three pointers don't point to allocated memory until you do something to make sure they are.

The advantage of my way is that the memory is allocated for you (on the stack). The disadvantage is that the strings are fixed to some constant size (100, in this case) at compile time.
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
And... and.... another point!

It would behoove you to use a meaningful variable name for your pointer array. "string[3]" is not a good name for the purpose you are using it for. "string_pointer[3]" or some such name would be better and be less confusing to yourself and the people who read your code.

Todd
 

ChrisA

macrumors G5
Jan 5, 2006
12,917
2,168
Redondo Beach, California
I was trying to. But there are still some mistakes which I can't find.. :(
Code:
	for(j=0;j<3;j++)
	{
		printf("String number %d: \"%s\"\n", j, string[j]);
	}
	return 0;
}

I see your error. You must think the "char *foobar[3]" is an array of three strings. No it is an array of three pointers. You need to alocate space for the actuall strings. Use eithe "srtdup", malloc, or calloc to make the space. See man pages.
 

Cheer

macrumors newbie
Original poster
Mar 18, 2008
5
0
Thank's a lot, i did it. I just have never realized that array *string[3] is an array of pointers, and that was my biggest mistake. Thanks a lot ;)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.