9 Replies - 271 Views - Last Post: 05 October 2012 - 03:45 AM Rate Topic: -----

#1 sanwall  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 07-March 10

array of pointer

Posted 05 October 2012 - 02:29 AM

The code is giving runtime error, explain please??? :dontgetit:

 
#include<iostream>
void main()
{
char  *p[3]={"Hi","to all"};
	for(int i=0;i<=2;i++)
		cout<<p[i]<<endl; 
}


Is This A Good Question/Topic? 0
  • +

Replies To: array of pointer

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,181
  • Joined: 21-June 11

Re: array of pointer

Posted 05 October 2012 - 02:37 AM

You declared an array containing three char*s, but only supplied two strings in your list. This means that the third one will be a null pointer. So when you're doing cout << p[2] << endl;, you're dereferencing a null pointer and thus invoking undefined behavior.

PS: The standard return type for main is int, not void.

This post has been edited by sepp2k: 05 October 2012 - 02:38 AM

Was This Post Helpful? 2
  • +
  • -

#3 sanwall  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 07-March 10

Re: array of pointer

Posted 05 October 2012 - 02:41 AM

THANKS :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#4 sanwall  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 07-March 10

Re: array of pointer

Posted 05 October 2012 - 02:51 AM

Initializing error here, why??? :dontgetit:

int *p[3]={1,2,3};
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: array of pointer

Posted 05 October 2012 - 02:52 AM

Are you just asking questions off of a test or quiz page?

What is p an array of, and what are you trying to use to initialize it?
Was This Post Helpful? 0
  • +
  • -

#6 sanwall  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 07-March 10

Re: array of pointer

Posted 05 October 2012 - 02:55 AM

I am just practicing array of pointers and could not understand the reason???
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: array of pointer

Posted 05 October 2012 - 03:02 AM

Answer this:

Quote

What is p an array of, and what are you trying to use to initialize it?

Was This Post Helpful? 0
  • +
  • -

#8 sanwall  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 07-March 10

Re: array of pointer

Posted 05 October 2012 - 03:12 AM

P is array of pointers and I am initializing to integers!!!
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: array of pointer

Posted 05 October 2012 - 03:17 AM

And...there's your answer.
Was This Post Helpful? 1
  • +
  • -

#10 sanwall  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 07-March 10

Re: array of pointer

Posted 05 October 2012 - 03:45 AM

Gr8 :clap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1