Thread: Basic Questions

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    5

    Basic Questions

    Hallo Guys

    I have some basic questions about C Programming, 'cause i'm not very good in it.

    Why is it impossible for an Array-initializing function to do the following ?
    Code:
     int[] init() { return int[32]; }
    Thanks for your help.

    Greez

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You can't return an array, you can only return a pointer.
    Also, you can't return a pointer to a variable that is local to the function, since that variable disappears when the function ends.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It's not impossible, it's just a matter of doing it properly. Your first problem is basic syntax, and your second is that you need to allocate heap memory for the array -- you cannot/should not return a "local" stack variable as those only last as long as the function call. So:
    Code:
    #include <stdlib.h>  /* for malloc() */
    #include <stdio.h> /* for printf() */
    
    int *rayfunc() {int *this=malloc(sizeof(int)*32); return this; }
    
    int main() {
    	int *ray=rayfunc(), i;
    	ray[31]=13;
    	for (i=0;i<32;i++) printf("%d\n",ray[i]);	
    	free(ray);  /* deallocate memory */
    	return 0;
    }
    Last edited by MK27; 05-17-2009 at 11:26 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    5
    Thanks.

    It's never ever possible to return an array?

    And another question:

    Why do the following ones do not return the same result?

    1. int *a = address; a++
    2. char *a = address; a++
    Last edited by Panda_; 05-17-2009 at 11:29 AM.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    > It's never ever possible to return an array?
    You shouldn't need to return an array anyway since arrays can be accessed by other functions than the one the array is declared in by passing pointers. Try to design your functions to accept a pointer to the first element as an argument, instead.


    > Why do the following ones do not return the same result?
    The char type is smaller than int, so ++ moves the address by a different number of bytes.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    5
    Thanks guys for your help.

    If you don't mind there's one more question:

    which type do variables from
    "int * a,b"
    have?

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Panda_ View Post
    It's never ever possible to return an array?
    Nope, because that would have to be a stack variable (get it?). You can only return a single value, either an address (ie a pointer, like to an array) or plain int/char/datatype.

    So one thing you can do to get around this is typedef a custom datatype:
    Code:
    #include <stdio.h>
    
    typedef struct {
    	int ray[32]; 
    } intray; 
    
    intray rayfunc() {
    	intray this = {{0}};
    	this.ray[31]=13;
    	return this; 
    }
    
    int main() {
    	int i;
    	intray example=rayfunc();
    	for (i=0;i<32;i++) printf("%d\n",example.ray[i]);
    	return 0;
    }
    However, the typedef has to be a struct or the array rules apply anyway.

    Also, if you want to pass an existing array to a function for modification, use a pointer.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Panda_
    which type do variables from
    "int * a,b"
    have?
    It would be wise to ask such questions with a code snippet posted in bbcode tags. If you are talking about the type of a and of b as declared by this line of code:
    Code:
    int * a,b;
    then the answer is that a is of type int* (pointer to int) and b is of type int. Because of a potential misinterpretation (one could accidentally or ignorantly consider b as being of type int*, I try to declare variables separately, especially if a pointer is involved.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    5
    Ok i got it.... thanks a lot.

    Does anybody know in which order this expression gets evaluated?
    Code:
    *&a++

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Panda_
    Does anybody know in which order this expression gets evaluated?
    With some exceptions, the order of evaluation of an expression is unspecified (but in this case there is nothing to vary). If we talk about the grouping of operators with respect to precedence, then it would be:
    Code:
    *(&(a++));
    Out of curiosity, but what motivated you to ask this question?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    Out of curiosity, but what motivated you to ask this question?
    Yeah, *& is kind of wacky or superfluous, no?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    With some exceptions, the order of evaluation of an expression is unspecified (but in this case there is nothing to vary). If we talk about the grouping of operators with respect to precedence, then it would be:
    Code:
    *(&(a++));
    Out of curiosity, but what motivated you to ask this question?
    Sounds like homework.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  13. #13
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by laserlight View Post
    With some exceptions, the order of evaluation of an expression is unspecified (but in this case there is nothing to vary). If we talk about the grouping of operators with respect to precedence, then it would be:
    Code:
    *(&(a++));
    I dont understand what is the problem in evaluating *&a++; As i see it, '*' and '++' has the same priority, with RtoL associativity thus a++ will be done first and as a result the whole expression should give the value at address '&a'. But when i run this code it doesn't even compiles. Why is it so?
    Code:
    #include<stdio.h>
    int main(void)
    {
    int a=10;
    printf("%d",*&a++);
    }
    Error 1 error C2102: '&' requires l-value
    When i tried to print
    Code:
    printf("%d",*&a);
    it gave the answer 10.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't take the address of a++, seeing it is not stored in memory anywhere. (Edit: If a is a pointer, and a++ is valid to dereference, you should be able to do &*a++.)

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by BEN10
    As i see it, '*' and '++' has the same priority
    Postfix operator++ has a higher precedence than unary operator*.

    Quote Originally Posted by BEN10
    the whole expression should give the value at address '&a'
    As I illustrated and as tabstop implied, the expression dereferences the address of the result of a++, but the result of a++ is an rvalue, which is why your compiler complained that "'&' requires l-value".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Some Basic questions
    By BlaX in forum C Programming
    Replies: 7
    Last Post: 06-30-2009, 09:51 AM
  3. Please help, basic unsigned conversion questions
    By ninjacookies in forum C Programming
    Replies: 3
    Last Post: 04-20-2005, 10:50 AM
  4. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM