question about pointers

This is a discussion on question about pointers within the C Programming forums, part of the General Programming Boards category; Hi, why if i call writelines all woks, but if i call *lineptr++ inside the main, it doesn't and i ...

  1. #1
    Registered User blob84's Avatar
    Join Date
    Jun 2010
    Posts
    46

    question about pointers

    Hi, why if i call writelines all woks, but if i call *lineptr++ inside the main, it doesn't and i get "error: lvalue...:
    Code:
    #include <stdio.h>
    	
    #define MAXLINES 1000
    
    void writelines(char *lineptr[], int nlines);
    
    int main(void)
    {
    	char *lineptr[MAXLINES];
    
    	int nlines;
    	for( nlines = 0; nlines < 5; nlines++)
    		lineptr[nlines] = "ciao\n";
    	
    	//writelines(lineptr, nlines);
    
            while (nlines-- > 0)
    		printf("%s\n", *lineptr++);
    }
    
    void writelines(char *lineptr[], int nlines)
    {
    	while (nlines-- > 0)
    		printf("%s\n", *lineptr++);
    }
    Last edited by blob84; 05-05-2011 at 04:56 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In main(), lineptr is the name of an array - yes, it has the address of the base element, but it's an array, and has a sizeof() that reflects it. (try it).

    In any other function, the array is sent as a pointer, and it's sizeof() becomes just the size of the pointer itself. You can always increment a pointer, but you can't increment the name of an array, before it has been "degraded" to a pointer.

  3. #3
    Registered User blob84's Avatar
    Join Date
    Jun 2010
    Posts
    46
    ok to let it work it should be assigned to a pointer of pointers, so in a function as Adak says *lineptr[ ] is **lineptr:
    Code:
    #include <stdio.h>
    	
    #define MAXLINES 1000
    
    void writelines(char *lineptr[], int nlines);
    
    int main(void)
    {
    	char *lineptr[MAXLINES];
    	char **h = lineptr;
    
    	int nlines;
    	for( nlines = 0; nlines < 5; nlines++)
    		lineptr[nlines] = "ciao\n";
    	
    	while (nlines-- > 0)
    		printf("%s\n", *h++);
    }
    Last edited by blob84; 05-05-2011 at 05:41 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 03:12 AM
  2. Pointers to pointers question
    By mikahell in forum C++ Programming
    Replies: 10
    Last Post: 07-22-2006, 01:54 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  4. question about pointers
    By kuwait in forum C++ Programming
    Replies: 6
    Last Post: 12-14-2002, 05:48 PM
  5. Pointers Question.....Null Pointers!!!!
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-28-2001, 11:13 PM

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