Thread: pointers issue

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    25

    pointers issue

    Hi

    this code is crashing, what did I do wrong in here?

    Code:
    #include <stdio.h>
    #include <string.h>
    #define  MAXLINES 100
    
    char *arrFilename[MAXLINES];
    
    void getFilenames(char **arrFilename, int limit)
    {
    	strcpy(arrFilename[0],"test");
    	
    }
    int main (void)
    {
    	getFilenames(arrFilename,MAXLINES );
    	printf("%s", arrFilename[0]);
    }

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    25
    I think I fixed the issue, I am getting these errors now, how can I correct this?

    c:\Nick\C projects\portal>cl filename2.c
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
    Copyright (C) Microsoft Corporation. All rights reserved.

    filename2.c
    filename2.c(28) : warning C4047: '=' : 'char **' differs in levels of indirection from 'int'
    filename2.c(32) : warning C4047: '=' : 'char *' differs in levels of indirection from 'int'
    Microsoft (R) Incremental Linker Version 10.00.30319.01
    Copyright (C) Microsoft Corporation. All rights reserved.

    /out:filename2.exe
    filename2.obj



    Code:
    #include <stdio.h>
    #include <string.h>
    #define  MAXLINES 100
    #define MAXSTRING 300
    
    char **arrFilename;
    
    void getFilenames(char **arrFilename, int limit)
    {
    	
    	strcpy(arrFilename[0],"test");
    	strcpy(arrFilename[1],"test test test"); 
    	
    }
    int main (void)
    {
    	int i;
    
    	// allocate space for sting array
    	arrFilename = malloc( MAXLINES * sizeof(char *));
    	for ( i = 0; i < MAXLINES; i++)
    	{
    		// allocate space for each line
    		arrFilename[i] = malloc(MAXSTRING * sizeof(char));
    	}
    
    	getFilenames(arrFilename,MAXLINES );
    	printf("%s\n", arrFilename[0]);
    	printf("%s\n", arrFilename[1]);
    }

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to include <stdlib.h> for malloc's prototype. Otherwise it's assumed to return an int, which is the source of the errors. You should turn up your compiler warning level. For gcc:
    gcc -Wall -Wextra yourprog.c
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    25
    thx, but if I forgot to include malloc function, why did it work anyway,

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by *nick View Post
    thx, but if I forgot to include malloc function, why did it work anyway,
    You didn't forget to include the malloc "function". You forgot to include the malloc "prototype". Without the prototype, the implementation makes an assumption that the function returns an int, issues a warning and compiles the code anyway. In this case (on your machine) the char* and char** pointers are the same size as an int, so the generated code worked.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by *nick View Post
    thx, but if I forgot to include malloc function, why did it work anyway,
    Undefined behaviour - which is what your code exhibited without the <stdlib.h> - is like that.

    Within the realm of undefined behaviour, anything is allowed to happen. One of the possibilities included within "anything" is behaviour that you interpret as "working". Other behaviours are possible as well.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by *nick View Post
    I think I fixed the issue, I am getting these errors now, how can I correct this?

    c:\Nick\C projects\portal>cl filename2.c
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
    Copyright (C) Microsoft Corporation. All rights reserved.

    filename2.c
    filename2.c(28) : warning C4047: '=' : 'char **' differs in levels of indirection from 'int'
    filename2.c(32) : warning C4047: '=' : 'char *' differs in levels of indirection from 'int'
    Microsoft (R) Incremental Linker Version 10.00.30319.01
    Copyright (C) Microsoft Corporation. All rights reserved.

    /out:filename2.exe
    filename2.obj



    Code:
    #include <stdio.h>
    #include <string.h>
    #define  MAXLINES 100
    #define MAXSTRING 300
    
    char **arrFilename;
    
    void getFilenames(char **arrFilename, int limit)
    {
        
        strcpy(arrFilename[0],"test");
        strcpy(arrFilename[1],"test test test"); 
        
    }
    int main (void)
    {
        int i;
    
        // allocate space for sting array
        arrFilename = malloc( MAXLINES * sizeof(char *));
        for ( i = 0; i < MAXLINES; i++)
        {
            // allocate space for each line
            arrFilename[i] = malloc(MAXSTRING * sizeof(char));
        }
    
        getFilenames(arrFilename,MAXLINES );
        printf("%s\n", arrFilename[0]);
        printf("%s\n", arrFilename[1]);
    }
    You could start by posting the actual code that gives you those warnings. Your code has only 30 lines but one of the warnings is for line 32.

    I also compiled with GCC with all warnings enabled and got a single warning (since you're not returning a value from main).

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    25
    that was the actual code minus top comments

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by christop View Post
    I also compiled with GCC with all warnings enabled and got a single warning (since you're not returning a value from main).
    Then you couldn't have had all warnings enabled since you should also have gotten:
    warning: unused parameter 'limit'
    and
    warning: implicit declaration of function 'malloc'
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by *nick View Post
    that was the actual code minus top comments
    Removing the top comments screwed up the line numbering. Still, the error lines were obvious.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by oogabooga View Post
    Removing the top comments screwed up the line numbering. Still, the error lines were obvious.
    Ah yes, I guess they should have been obvious to me. Too much coding makes me not see to well. :/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers issue
    By quo in forum C++ Programming
    Replies: 25
    Last Post: 05-12-2012, 03:29 PM
  2. Issue with arrays, pointers, and structs.
    By RexInTheCity in forum C Programming
    Replies: 5
    Last Post: 03-29-2010, 03:30 PM
  3. Pointers Issue
    By Bakster in forum C Programming
    Replies: 9
    Last Post: 08-30-2009, 02:22 PM
  4. pointers and arrays issue
    By KoshiB in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2006, 10:36 AM
  5. array of pointers issue
    By Brain Cell in forum C++ Programming
    Replies: 8
    Last Post: 03-27-2005, 04:18 PM