الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Array of pointers

مغلق
بدأه maro009 في 12 مارس 2005 · 23 رد · 1,627 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

ارجو يا اخوانى مساعدتى فى هذا الموضوع

Create a pointer array that will point to the string characters. The size of the pointer array should be as large as the size of the string

( [] void InitializePA(char* [], char

so it mean than i creat an array with Pointer and each pointer point to an element of the string

#2

salam

InitializePA(char* S, char Str[size])
     {   char c;
         S = Str;  
     //we point the begining of str to  S which mean that S now point to my array 
    //and it will take the same size
         while ((c = getchar()) != '\n') 
           {
              *S = c;
              S++;        
         }
     }

best regards

#3

i don't know what to say

thank you very much

#4

Mark222

can you explain more cuz i don't understand from where you got

(()c = getchar)

and how you know they will have the same size

#5

السلام عليكم

اخى ماره انا لا اعتقد ان حل الاخ ماركو صحيح, إليك محاولتى ولكن للاسف ليس لدى مترجم سى كى اجربها:

void InitializePA(char* []cp, char c[]){   
 char* cptmp;  
        while (cptmp++=&c++)
          {
             cp++ = cptmp;
        }
    }

جرب حظك.

ملاحظه : هناك معلومه ناقصه فى السؤال الا وهى حجم ال array الذى يحتوى النص, لذالك هذا الحل ليس كامل.

والسلام

تم تعديل هذه المشاركة بواسطة احمد غريب في 13 مارس 2005 في 05:58

لا إله إلا الله محمد رسول الله

busbar : يجب ان تدرك انه هناك حد ادنى للمعرفة المطلوبة قبل البدء في عمل أي شئ.

#6

لنضع اسماء للمتغيرات:

void InitializePA(char* a[], char s[])

الآن a هو اسم مصفوفة المؤشرات (array of pointers ), المطلوب عمله هو أن كل مؤشر في a يجب أن يشير الى ما يقابله من الأحرف في الـ string اللذي أسميناه s

ممكن نعمل loop يكون شرط الخروج منها هو ان يكون العنصر الحالي 0 (الـ string هنا هو عبارة عن array و الـ array ليس له حجم في الحقيقة داخل هذا الـ function لأنه ليس سوى pointer, الطريقة اللتي تحدد بها انتهاء الـ string هي بوضع الرمز '\0' و اللذي هو ليس في الحقيقة سوى رقم 0 )

في هذه الـ loop لنفرض ان رقم المتغير هو i, عندئذ نريد العنصر [a[i و اللذي هو بوينتر, أن يشير الى [s[i

أعتقد هذا ما نريده:

a = &(s);

أو حتى

a = (s+i);

لكني لست متأكدا %100

تم تعديل هذه المشاركة بواسطة hasan_aljudy في 13 مارس 2005 في 07:13

#7

اخى احمد غريب

the string is there but i didn't add all the assigment

#8

طيب اخى ماره ضع المسئله كامله لان حجم الاسترنج لابد ان يكون معروف حتى تستطيع التوقف عند نهايته.

لا إله إلا الله محمد رسول الله

busbar : يجب ان تدرك انه هناك حد ادنى للمعرفة المطلوبة قبل البدء في عمل أي شئ.

#9

يا اخوانى سوف اضع التمرين كاملا مع المرفقات

وهذا ما توصلت اليه ,,,

1- heder file

#ifndef __letters_h
#define __letters_h

const int Size   = 100;
const int tempSize=100;

void clearbuffer();
void readString    (char *str ,const int Size);
bool checkString   (char *str);
bool duplicated    (char *str);
char *searchlastA  (char *str);
char *searchlastN  (char *str);
void last_character_menu();


//Optional part:
void InitializePA(char *pointers[], char string[]);

#endif

2- cpp file

#include "letters.h"
#include <cstring>
#include <iostream>

using namespace std;


//________________________________________________________________________________________________
//¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
                         /// this function is to clear the buffer

void clearbuffer()
{
	char k;
	do
	{
  cin.get(k);

	}while(k != '\n');

}
//-------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
                               //this function will read the String

void readString(char *str ,const int Size)
{
	cin.get (str , Size);
	clearbuffer();


}
//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------

                   // this function will test if the inputs are unique characters and numbers

bool checkString(char *str)
{
  
	if (str)
	{
     int count =0;
  for (int i=0; i<strlen(str); i++)
  {
 	 if ( *(str+i) == 32 || *(str+i)>=48 && *(str+i)<=57 || *(str+i)>=65 && *(str+i)<=90 || *(str+i) >=97 && *(str+i)<=122)
    {
   	 
   	 count++;
   	 
   	 
    }
  }

  if (strlen(str) < 10)
  {
 	 cout<<"\nthe string must have at least 10 characters"<<endl;
 	 return false;
  }

  else if (count == strlen(str))
  {
 	 
 	 return true;
  }

  else
  {
 	 
 	 cout<<"\nthe string must unique characters and numbers"<<endl;  
 	 return false;
  }
  
	}

	else
	return false;
}
//-------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------
                   // this function will check if ther are duplicated characters.

bool duplicated(char *str)
{
	char temp[tempSize];// creat temp string

	strcpy (temp , str );

	for (int i=0; i<strlen(temp); i++) // to add all letters to the uppercase so i can find the duplicated
	{
  *(temp+i) = toupper(*(temp+i));
  

	}


	for (int j=0; j<(strlen(temp))-1; j++)// will sort the temp string
	{
  for (int k=0; k<(strlen(temp))-1; k++)
  {
 	 if (*(temp+k) > *(temp+(k+1)))
 	 { 
    char tempo    = *(temp+k);
                *(temp+k)     = *(temp+(k+1));
    *(temp+(k+1)) = tempo;
 	 }
 	 
  }
	}


	for (int m=0; m<strlen(temp); m++)
	{
  if (*(temp+m) !=32)/// here to ignore white space 
  {
 	 if ( *(temp+m) == *(temp + (m+1)) )
 	 {
    cout<<"\nyour string has duplicated characters"<<endl;
    return false;
 	 }
  }
	}

  return true;


}

//_____________________________________________________________________________________________
//---------------------------------------------------------------------------------------------

          //this function will look for the last alphabetical character and return it's value and address 

char *searchlastA(char *str)
{
	if (str)
	{
  int count = 1;
  
  for (int i=0; i<strlen(str); i++)
  {
 	 // next while loop will test if the last character is alphabetical character .
 	 while (   *(str+(strlen(str)-count)) ==32 || *(str+(strlen(str)-count)) >=48 && *(str+(strlen(str)-count))<=57   )
 	 {
    count++;
    continue;
 	 }
 	 
  }
  // next if statment will check if the string contain alphabetical character
  if (count >= strlen(str))
  {
 	 cout<<"your string does not contain alphabetical character..."<<endl;
 	 
 	 return NULL;
  }
  else
  {
 	 return (str+(strlen(str)-count));
  }
  
	}
	else 
	{
  return NULL;
	}
	return NULL;
}

//---------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------
              //this function will look for the last numeric character and return it's value and address 


char *searchlastN(char *str)
{
if (str)
	{
  int count = 1;
  
  for (int i=0; i<strlen(str); i++)
  {
 	 // next while loop will test if the last character is numeric character .
 	 while (   *(str+(strlen(str)-count)) < 48 || *(str+(strlen(str)-count)) > 57   )
 	 {
    count++;
    continue;
 	 }
 	 
  }
  // next if statment will check if the string contain numeric character
  if (count >= strlen(str))
  {
 	 cout<<"your string does not contain numeric character..."<<endl;
 	 
 	 return NULL;    
  }
  else
  {
 	 return (str+(strlen(str)-count));
  }
  
	}
	else 
	{
  return NULL;
	}
	return NULL;
}

//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
                          // last character menu 
void last_character_menu()
{
	cout <<"choose one from the following :\n\n";
	cout <<"	1- Last alphabetical character.."<<endl;
	cout <<"	2- Lsat numeric character.."<<endl;
}

//__________________________________________________________________________________

3-main file

#include <iostream>
#include <stdlib.h>
#include "letters.h"
using namespace std;

int main ()
{
	char string[Size];
	char *str=string;
	char again ='n';
	char *lastchar;
	int choose = 0;


        cout<<"\n";
  cout<<"  *******************************************************************\n";
  cout<<"  *  Please add string that contain unique characters and numbers.  *\n";
  cout<<"  *  the string must contain at least 10 characters.                *"<<endl; 
  cout<<"  *  the string must not contain duplicated characters.             *"<<endl;
  cout<<"  *  (white space is allowed)                                       *\n";
  cout<<"  *******************************************************************\n\n";




	do 
	{
  

  cout<<"Your String : ";
    
  readString (str , Size); //  read string

  if (checkString(str) && duplicated(str ) )  // check if the inputs are unique characters and numbers and that there are no duplicated characters.
  {

 	 cout<<"\nyour string is ok"<<endl;
 	 cout<<str<<endl;
 	 //---------------------------------------------------
           // in this part i return the last character value and address.
 	 last_character_menu();
 	 cout<< "your choice is :";
 	 cin>>choose;
 	 cout<<endl;
 	 switch (choose)
 	 {
    case 1 : lastchar = searchlastA(str);  // ?? here i use char pointer i didn't use function pointer to point to this function 
   	 break;
    case 2 : lastchar = searchlastN(str); // ex : normaly i should initialize it this way "char *lastchar (char*)??? 
   	 break;
 	 }
 	 if (lastchar)
 	 {
    cout<<"\nlast character is :<"<<*lastchar<<">"<<endl;
    cout<<"and last character address is :<"<<&lastchar<<">"<<endl;
 	 }
 	 else
 	 {
    cout<<"";
 	 }
 	 //________________________________________________________________________________
 	 //--------------------------------------------------------------------------------
        
 	 
 	 
 	 //---------------------------------------------------------
  }
  else
  {
 	 cout<<"your string doesn't match the rules \n\n";
 	 
  }
  cout<<"\ntry again .... (Y/N) ?"<<endl;


  
  cin>>again;
  clearbuffer();
	}while(again !='n');




	return 0;
}

al are working just missing the part of array of pointers

i hope it's more clear now

and some one can find me the solution

thanks

Lab_16_exercise.doc

#10

ملحوظة يا جماعة

انا اعمل بلغة c++

فقط

#11
void InitializePA(char *pointers[], char string[]){ 
int i=0; 
char* x=searchlastA(&string);
char* y=pointers; 
       while ((y+i)!=x)
         {
            pointers++ = &string;
i++;
       }
   }

اتوقع ان يكون الحل والله اعلى واعلم

والسلام

تم تعديل هذه المشاركة بواسطة احمد غريب في 13 مارس 2005 في 18:14

لا إله إلا الله محمد رسول الله

busbar : يجب ان تدرك انه هناك حد ادنى للمعرفة المطلوبة قبل البدء في عمل أي شئ.

#12

تحياتي للجميع

اخي مارو بالعودة الى الكود الذي كتبته لك في بداية السؤال وبغض النظر عما كتب بعد

انا لا اقصد شيء من وضع getchar() في المثال ولكن على فرض انه تريد ادخال الاحرف والبرنامج يستقبلها لكن ما اريده هو ان اضع مؤشر الاراي االى البونتر كي يشير البونتر الى بداية الاراي مثل الصورة التالية

Default.asp?FileName=figure.gif

#14

يا اخوانى لقد اجريت هذه التجربة على array of ointers وهى ليست لها علاقة بالبرنامج

ارجو ان تخبرونى اذا كانت مسموح بها

int main()
{ 
     char array[]="Try To Print This";
     int m =strlen(array);
     const int s = m;
     char *pointer;

     for (int i=0; i<s; i++)
     {
          pointer = &array;
          
          cout<<pointer<<endl;
        
     }
      cout<<"now with pointers"<<endl;
     for (int k= 0; k<s; k++)
     {
        
         cout << k<<"- "<<*pointer[k]<<endl;
         
     } 
     

  system("PAUSE");	
  return 0;
}
#15

السلام عليكم

نعم مسموح بها على حد علمى, فالحلقه الاولى ستقوم بكتابة العانوين والحلقه الثانيه ستقوم بكتابة الاحرف.

سؤال فقط لماذا إستخدمت m و s كان يكفى ان تكتب const int m... بدلاً من إستخدام عنصرين.

والسلام

لا إله إلا الله محمد رسول الله

busbar : يجب ان تدرك انه هناك حد ادنى للمعرفة المطلوبة قبل البدء في عمل أي شئ.

#16

الف شكر لكم يا اخوانى على مساعدتكم

الاخ احمد غريب معك حق

اشكرك على المتابعه

#17

السلام عليكم

هناك حل اخر للمشكله السابقه سبحان الله كيف راح من بالى :

void InitializePA(char *pointers[], char string[]){
int i = sizeof string/sizeof *string
for(j=0;j<i;j++){
                 pointers++ = &string[j];

      }
  }

جرب هذا الحل فهو افضل من الذى سبق ويمكن ان تستخدمه لانواع اخرى من المعطيات.

والسلام

لا إله إلا الله محمد رسول الله

busbar : يجب ان تدرك انه هناك حد ادنى للمعرفة المطلوبة قبل البدء في عمل أي شئ.

#18

الـ sizeof لن تنفع هنا لانها لا تنفع مع الـpointers فهي لا تعمل إلا إذا كان حجم المتغير معلوما وقت الـcompile time

#19

لاحظ ان sizeof إذا طبقناها على متغير من نوع pointer تعطيق حجم نوع المتغير وليس حجم الarray وعندما نستخدم sizeof على array تعطينا حجم الarray بالبايت.

لذالك الطريقه صحيحه بإذن الله.

والسلام

تم تعديل هذه المشاركة بواسطة احمد غريب في 14 مارس 2005 في 02:19

لا إله إلا الله محمد رسول الله

busbar : يجب ان تدرك انه هناك حد ادنى للمعرفة المطلوبة قبل البدء في عمل أي شئ.

#20

لكنك عندما ترسل array الى function فإنك في الحقيقة ترسل pointer و في هذه الحالة من المستحيل معرفة حجمه إلا إذا ارسلت متغير آخر الى الـfunction به حجم الـ array.

يمكن الـ sizeof ينفع على الـ array إذا كان statically allocated لأن حجمه سيكون معروفا وقت الـ compile time لكن هذا لا ينطبق على إرسال array الى function.

عموما انا لاحظت من الكود ان الحجم هو constatnt و هو 100 لهذا المسألة المفروض محلولة من هذه الناحية.

#21

السلام عليكم

معك حق اخى الحبيب المفروض عملية تحديد حجم الارى يحدث قبل إستدعاء الداله, انا فهمتك خطاء وظننت انك معترض على حجم ال poiner لان حجم البونتر ثابت.

انا لا املك كمبايلر لاجرب وإنما احاول الاعتماد على الذاكره, إن شاء الله حجيب كمبايلر عن قريب.

والسلام

لا إله إلا الله محمد رسول الله

busbar : يجب ان تدرك انه هناك حد ادنى للمعرفة المطلوبة قبل البدء في عمل أي شئ.

#22

السلام عليكم يا اخوانى

لقد وجدت الحل وهو بسيط جدا جدا,

void InitializePA(char* pointers [], char string [])
{
	for (int i=0; i< strlen(string); i++)
	{
  pointers= &string;
	}

}

and it works

and i test the size of the pointers array and i found that it has the same size of the string array

:)

#23

I dont know what's the exact answer you want, but here you are one of the solutions:

void Initialize(/*Out Parameter*/ char ** array,const char * String)
{
*array = new char[strlen(String)+1];
strcpy(*array,String);
}

and here you're how to call it:

char * temp;
char * mystring = "My name is anything";
Initialize(&temp,mystring);
.
.
.
.
delete [] temp;

and there're many solutions according to your design and your point of view

Mohamed Meshref

Performance Engineer

SQL Server Engine Team

Microsoft Corporation

Redmond, Washington, USA

http://www.microsoft.com/sql/

Blog: http://www.mmeshref.com

#24
maro009 كتب:
السلام عليكم يا اخوانى

لقد وجدت الحل وهو بسيط جدا جدا,

void InitializePA(char* pointers [], char string [])
{
	for (int i=0; i< strlen(string); i++)
	{
  pointers= &string;
	}

}

and it works

and i test the size of the pointers array and i found that it has the same size of the string array

:)

هذا هو ما كنت أحاول قوله,

هذا الموضوع مغلق.

مواضيع مشابهة