Thread: Question about array from a newbie

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463

    Question about array from a newbie

    Hi all,
    I'm learning C and running into this problem with array that I try to understand. When I try the following code to print an array of 5 interger:
    for(i = 0; i<5; i++)
    {
    printf("%.1lf %.1lf %.1lf \n", source[i], target1[i], *target2++);
    }
    I get the error "lvalue required as increment operand" for using *target2++. However, if it use: *(target2+i), then i'm fine. Can someone explain to me why I can't use increment to print like that?

    Thanks alot in advance.

  2. #2
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    target2 is an array?

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by Babkockdood View Post
    target2 is an array?
    Yeah. I declare it: int target2[5];

    Here's my code:

    Code:
    #include <stdio.h>
    
    void copy_arr(const double source[], double target[], int COLS);
    void copy_ptr(const double source[], double target[], int COLS);
    int main(void)
    {
    	int i;
    	double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
    	double target1[5], target2[5];
    	
    	copy_arr(source, target1, 5);
    	copy_ptr(source, target2, 5);
    	printf("source     target 1      target 2\n");
    	for(i = 0; i<5; i++)
    	{
    		printf("%.1lf        %.1lf              %.1lf \n", source[i], target1[i], *target2++); // *target2++ gives compile error. 
    		
    	}
    	return 0; 
    }
    
    void copy_arr(const double source[], double target[], int COLS)
    {
    	int i; 
    	for (i=0; i < COLS; i++)
    		target[i] = source[i];
    }
    
    void copy_ptr(const double source[], double target[], int COLS)
    {
    	int i;
    	for (i=0; i<COLS; i++)
    	{	
    		*target++ = *source++;
    	}
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Array names are not pointers, so you cannot increment them.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Like quzah said, array names are constants, not variables. Can't touch them.

    You've probably read that array name is the address of the first element of the array (e.g. arrayName == &arrayName[0]), which can get one thinking that it is equivalent to a pointer when it is not.

  6. #6
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by msh View Post
    Like quzah said, array names are constants, not variables. Can't touch them.

    You've probably read that array name is the address of the first element of the array (e.g. arrayName == &arrayName[0]), which can get one thinking that it is equivalent to a pointer when it is not.
    Thank you. I think I kinda of understand. But how come this statement work: "*target++ = *source++;"

  7. #7
    Novice
    Join Date
    Jul 2009
    Posts
    568
    In function definitions, AND ONLY, type arrayName[] and type* arrayName are equivalent.

    E.g., both of the below definitions are equivalent.
    Code:
    void copy_ptr(const double source[], double target[], int COLS)
    {
        // Code
    }
    
    void copy_ptr(const double* source, double* target, int COLS)
    {
        // Code
    }
    Look at the arguments you call your functions with and what they represent, and think about it.

  8. #8
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    You can't increment an array itself. You can increment a value in an array like so.

    Code:
    printf("%.1lf %.1lf %.1lf \n", source[i], target1[i], target2[i]++);

  9. #9
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by msh View Post
    In function definitions, AND ONLY, type arrayName[] and type* arrayName are equivalent.

    E.g., both of the below definitions are equivalent.
    Code:
    void copy_ptr(const double source[], double target[], int COLS)
    {
        // Code
    }
    
    void copy_ptr(const double* source, double* target, int COLS)
    {
        // Code
    }
    Look at the arguments you call your functions with and what they represent, and think about it.
    I think I got it. An array name is like a constant pointer, so "target2++" would change the address, so it's not allowed. However, when I pass the address &target2[0] into a function, in the scope of the function, it becomes variable pointer thus i can use increment with it.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    An array is not a pointer.
    That is an important concept to grasp.
    With a few exceptions, the standard says that when you put the array's name somewhere, it will decay to a pointer to its first element. Passing an array to a function is not one of those exceptions, so it decays. It's that simple.
    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.

  11. #11
    Registered User op@op's Avatar
    Join Date
    Aug 2010
    Posts
    4
    Quote Originally Posted by Elysia View Post
    An array is not a pointer.
    That is an important concept to grasp.
    With a few exceptions, the standard says that when you put the array's name somewhere, it will decay to a pointer to its first element. Passing an array to a function is not one of those exceptions, so it decays. It's that simple.
    Ok, so it decays to a pointer, but that did not answer the user's question and I am interested since I have a similar question.

    Why is it ok in the following case? Due to the fact that arr is a copy that belongs to foo()? Output is 1 2 3 4
    Code:
    #include <stdio.h>
    
    void foo(int * arr) {
    	int i;
    	for(i = 0; i < 4; ++i) {
    		printf("%d ", *arr);
    		arr++;
    	}
    }
    
    int main(void) {
    
    	int arr[] = {1, 2, 3, 4};
    	foo(arr); // Decays to a pointer to the first element
    			  // in arr[], which is 1
    
    	return 0;
    }

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's all about pointers. The scope is irrelevant. I can do this in main, in foo, or anywhere I want. So long as it's a pointer.
    Code:
    int arr[] = {1, 2, 3, 4};
    arr++; // Illegal
    int * p = arr;
    p++; // Legal
    When you pass arr to a function, it will decay to a pointer.
    The whole int arr[] syntax inside prototypes is just eye candy. It's a pointer. Always.
    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.

  13. #13
    Registered User op@op's Avatar
    Join Date
    Aug 2010
    Posts
    4
    Ah I understand with the code example, thanks

  14. #14
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    I understand it now. Thank you very much guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie question about 'sizeof' and arrays
    By Sharke in forum C Programming
    Replies: 27
    Last Post: 12-09-2009, 06:30 AM
  2. Embaracing Array question
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 04-06-2009, 10:29 AM
  3. Quick Question: 2D Array with char **
    By Phoenix_Rebirth in forum C Programming
    Replies: 4
    Last Post: 01-29-2009, 07:33 AM
  4. newbie array question
    By chris1985 in forum C Programming
    Replies: 3
    Last Post: 05-05-2005, 03:43 AM
  5. Replies: 5
    Last Post: 05-30-2003, 12:46 AM