ANSI C parsing char array as pointer

This is a discussion on ANSI C parsing char array as pointer within the C Programming forums, part of the General Programming Boards category; Hey guys. Im experiencing problems trying to parse a char array as a pointer to a function i have defined ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    1

    ANSI C parsing char array as pointer

    Hey guys.

    Im experiencing problems trying to parse a char array as a pointer to a function i have defined in my code. This code is being written purely for self learning purposes though the wall i have hit is driving me nuts. Ive checked out the tutorials and i cant seem to find an answer to this unless ive overlooked it. Im using the gcc (c99) compiler.

    heres the code i have so far:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void read_string(char *);
    
    int main(int argc, char *argv[])
    {
    	char forename[30], surname[30], banner_id[9], programme[50];
    	char a = 'a';
    	int stage = 0;
    	
    	printf("Please type your forename:\n");
    	//scanf("%30s", &forename);
    	read_string( &forename);
    	printf("Please type your surname:\n");
    	scanf("%30s", &surname);
    	
    	printf("Please type your banner ID:\n");
    	scanf("%9s", &banner_id);
    	
    	printf("Please type your programme of study:\n");
    	scanf("%50s", &programme);
    	
    	printf("Please type your year of study:\n");
    	scanf("%d", &stage);
    	
    	printf("\n%s %s %s\n%s\n%d\n\n", forename, surname, banner_id, programme, stage);
    	
    	return 0;
    }
    
    void read_string(char *l_string)
    {
    /*	char character;
    	int index;
    	for (index = 0; index < (sizeof(l_string) * sizeof(char)); index++)
    	{
    		scanf("%c", &character);
    		if (character == EOF)
    		{
    			return;
    		}
    		else
    		{
    			l_string[index] = character;
    		}
    	}
    	return;
    */}
    Originally i had all my inputs as scanf, then i encountered a problem whereby if i entered spaces it messed everything up, i dont see this as a problem because while exploring other options i encountered something i find even more mind boggling.

    I get the compiler error of:
    Quote Originally Posted by C99 Compiler
    gcc -o q2/main q2/main.c
    q2/main.c: In function ‘main’:
    q2/main.c:34: warning: passing argument 1 of ‘read_string’ from incompatible pointer type
    So i have tried the following corrections to no avail: (prototype first then implementation)
    void read_string(char *);
    void read_string(char *l_string)

    void read_string(char *[]);
    void read_string(char *l_string[])

    void read_string(char *);
    void read_string(char *l_string[])

    void read_string(char[] *);
    void read_string(char *l_string[])

    none of which seem to work. So question is, how do i pass in my char array as a pointer and not get these errors?

    the test call for the function is the line: read_string( &forename);

    (of course the function is commented out, i was trying to reduce the possibilities of what was causing this problem)

    Thanks in advance for any help.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    When you call read_string, you don't put the ampersand.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    In fact, all of your &#37;s scanf calls are silently wrong as well.

    Use this
    gcc -W -Wall -ansi -pedantic -O2 -o q2/main q2/main.c

    That will also (in addition to many other things) diagnose incorrect printf/scanf usage.

    So
    scanf("%30s", forename);
    read_string( forename);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Warning 1 warning C4100: 'argv' : unreferenced formal parameter g:\w00t\Visual Studio 2008\Projects\Temp\Temp4.cpp 6
    Warning 2 warning C4100: 'argc' : unreferenced formal parameter g:\w00t\Visual Studio 2008\Projects\Temp\Temp4.cpp 6
    Warning 3 warning C4189: 'a' : local variable is initialized but not referenced g:\w00t\Visual Studio 2008\Projects\Temp\Temp4.cpp 9
    Warning 4 warning C4100: 'l_string' : unreferenced formal parameter g:\w00t\Visual Studio 2008\Projects\Temp\Temp4.cpp 32
    Warning 6 warning C6067: Parameter '2' in call to 'scanf' must be the address of the string g:\w00t\visual studio 2008\projects\temp\temp4.cpp 16
    Warning 8 warning C6067: Parameter '2' in call to 'scanf' must be the address of the string g:\w00t\visual studio 2008\projects\temp\temp4.cpp 19
    Warning 10 warning C6067: Parameter '2' in call to 'scanf' must be the address of the string g:\w00t\visual studio 2008\projects\temp\temp4.cpp 22
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM

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