9 Replies - 229 Views - Last Post: 01 March 2018 - 07:08 AM Rate Topic: -----

#1 Lillehh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-February 18

Pointer in C programming

Posted 28 February 2018 - 10:11 PM

Hello, all experts in c

I have question about pointer

Can explain to me how to get the:
c values

C=10
C=15
C=22
C=8
C=9
C=10
Is This A Good Question/Topic? 0
  • +

Replies To: Pointer in C programming

#2 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2619
  • View blog
  • Posts: 4,175
  • Joined: 21-June 11

Re: Pointer in C programming

Posted 28 February 2018 - 10:21 PM

What? Could you please describe further what you're trying to do and what problems you're facing?
Was This Post Helpful? 0
  • +
  • -

#3 Lillehh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-February 18

Re: Pointer in C programming

Posted 28 February 2018 - 10:36 PM

hai sepp2k
so here the code

so i type the code in devc++

and the output is

C=10
C=15
C=22
C=8
C=9
C=10

now i need to explain in handwritten how get the c value..

to seep2k

#include<stdio.h>

int main()
{
	int a=3,b=7,c[7],i=3;
	
	printf("c = %d",*c=a+b );
	printf("\nc = %d",*(c+1)=c[0]+5);
	printf("\nc = %d",*(c+2)=b+c[1]);
	
	while(i<6)
	{
		printf("\nc = %d",*(c+i)=i+5);
		i++;
	}
}

This post has been edited by ndc85430: 01 March 2018 - 02:27 AM
Reason for edit:: Removed code in large font and added code tags.

Was This Post Helpful? 0
  • +
  • -

#4 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 825
  • View blog
  • Posts: 3,337
  • Joined: 13-June 14

Re: Pointer in C programming

Posted 01 March 2018 - 02:30 AM

So, what is your understanding of how the code works? Please make sure to ask specific questions; it makes no sense for people to guess where you're stuck. Also, please remember to post code within "[CODE]" tags as you're asked in the box where you type your post. You can use the "[CODE]" button in the editor to insert these and then just paste your code between them. I've done it for you this time.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg   User is offline

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,063
  • Joined: 25-December 09

Re: Pointer in C programming

Posted 01 March 2018 - 02:49 AM

You appear to be trying to access your array with pointer notation, why? It is easier and usually less error prone to access arrays using array notation (array[1]).

Also it would be easier to "see" what is happening if you do the calculations before the printf() and just print the results of the calculation with printf().

Lastly, for now, why the magic number 6 in your loop? You only initialized three elements so printing any of the other would produce undefined values. Also Since you declared your array with a size of 7 there are 7 elements available in your array, not 6.


Jim
Was This Post Helpful? 0
  • +
  • -

#6 Lillehh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-February 18

Re: Pointer in C programming

Posted 01 March 2018 - 03:09 AM

thank you ndc85430n & jim...

okay..let me clear on this, that is one of my past year questions paper, i need to revise on that question..

i do understand array have memory stack

it given

a = 3
b = 7

c[7] = so this part i dont understand : as no value in it, am i correct?

so the

line 7 : *(c+1)=c[0]+5);

line 7 : *(c+1)=c[0]+5)
: *(c+1)= ?? + 5
: c[0] is what value ??
Was This Post Helpful? 0
  • +
  • -

#7 Lillehh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-February 18

Re: Pointer in C programming

Posted 01 March 2018 - 03:15 AM

sorry my mistakes

line 7 : *c=a+b
: *c = 3 + 7
: *c = 10 (am i correct)

line 8 : *(c+1) = c[0]+5)
: *(c+1) = c[0] + 5
: = ?? + 5
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg   User is offline

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,063
  • Joined: 25-December 09

Re: Pointer in C programming

Posted 01 March 2018 - 03:19 AM

Quote

c[7] = so this part i dont understand : as no value in it, am i correct?

The c[7] is creating an array of int, each element of that array has an undefined value (uninitialized).

This is where some of the confusion is coming into play. The following function is doing more than it could.
    printf("c = %d",*c=a+b );


*c = a + b;
printf("c = %d", *c);



So let's break down the first line.

Do you understand what *c = a + b; is actually doing? By the way this is considered pointer notation.

Assuming no, what about c[0] = a + b;? By the way this is considered array notation.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 Lillehh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-February 18

Re: Pointer in C programming

Posted 01 March 2018 - 03:38 AM

printf("c = %d",*c=a+B)/>;


i understand
so c = 10 ??

then c = 10 use in line 8 ??

printf("\nc = %d",*(c+1)=c[0]+5);


Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg   User is offline

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,063
  • Joined: 25-December 09

Re: Pointer in C programming

Posted 01 March 2018 - 07:08 AM

Quote

i understand
so c = 10 ??

No c[0] is equal to 10. Or in pointer speak *c is equal to 10.


Quote

then c = 10 use in line 8 ??

    printf("\nc = %d",*(c+1)=c[0]+5);

No on line 8 you're dealing with *(c + 1) in pointer speak or c[1] in array speak. So this should print 15 since c[0] is equal to 10 at this point.

Did you try putting those statements into a program and actually run that program?

You still don't seem to grasp that c is an array of int, not a single int.

By the way *(c + 1) would not be the same as *c + 1 the parentheses use and placement is important in this content.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1