pointers

This is a discussion on pointers within the C Programming forums, part of the General Programming Boards category; I keep getting an error when trying to assign my pointer to the arrays I have declared. The error is ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    42

    pointers

    I keep getting an error when trying to assign my pointer to the arrays I have declared. The error is 'char *' differs in levels of indirection from 'char (*)[100]'. I am following the syntax in the book.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define str_length 100
    int main()
    {
    
    	char str1[100];			/* array for first string */
    	char str2[100];			/* array for second string */
    	char *pstr1 = NULL;	/* pointer to str1 */
    	char *pstr2 = NULL;	/* pointer to str2 */
    	int count1;				/* length of str1 */
    	int count2;				/* length of str2 */
    	char more ;				/* variable to continue with the loop y||Y */
    	int i = 0;				/*loop counter */
    
    
    
    	do
    	{
    	pstr1 = &str1;  /* get the address of str1 */
    	pstr2 = &str2;	/* get the address of str2 */
    
    	printf("Please enter the first word\n"); /* ask for user input */
    
    	  scanf("%s", pstr1);							 /* take the input into the buffer */
    	
    
    	count1 = strlen(pstr1);					 /* get the length of the string */
    		
    	printf("Please enter the second word\n"); /* ask for user input */
    
    	  scanf("%s", pstr2);						  /* take the input into the buffer */
    		
    	count2 = strlen(pstr2);					 /* get the length of the string */
    	
    	
    	/* concatenate str2 to end of str1 */
    	if(count1 == count2 ||count1 < count2 )
    	{
    	for(i = 0; i < count1; i++)
    		printf(" you entered %c\n", *(*pstr1 + i));
    	/*printf(" %s %s and \n", pstr1,pstr2);
    			
    	printf(" %s %s\n",strrev(pstr1), pstr2);*/
    	
    	}
    	else
    	/* concatenate str1 to end of str2 */
    	{
    	printf(" %s %s and \n", pstr2,pstr1);
    
    	printf(" %s %s \n",strrev(pstr2),pstr1);
    	
    	}
    	
    	printf(" Would you like to continue? y or n:\n"); /* ask user if they want to enter more data */
    
    	scanf("%c", &more);									/* read the input */
    
    	
    	while(getchar() != '\n') ; /* flush input buffer it appears that the scanf leaves a \n inthe buffer */
    	
    	}while(more == 'y' || more == 'Y'); /*Test to continue or not*/
    	
    	return 0;
    	
    	
    }

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,263
    If you change [begin] and [\end] to &#91;code] and &#91;/code] your code formatting will be preserved. Also:-
    Code:
    pstr1 = &str1; /* get the address of str1 */
    pstr2 = &str2; /* get the address of str2 */
    str1 and str2 are addresses already. They're pointers to the character array. It's only when used with array indexes or the indirection operator (*str1) that they reference something. That should be:-
    Code:
    pstr1 = str1; /* get the address of str1 */
    pstr2 = str2; /* get the address of str2 */

  3. #3
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    First of all use the code tags: [code] and [/code]

    Secondly, why even use pointers to the strings? Why not just reference the arrays directly in your statements?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21