would i have to copy pointers? (urgent?) | Bytes (2024)

Home Posts Topics Members FAQ

billyjay777

would i have to copy pointers? (urgent?) | Bytes (1) 7 New Member

Hi,
I have a project where i must use pointers and write a function that strips all the white spacebar spaces out of an array. I also have to count how many white spaces i have. Well i figured out how to count all the white spaces i just don't know how to strip all the white spaces out of the sentence entered. I asked the proffessor and he said one way of doing it would be to crreate another array and copy it to the new array and when it hits a white space not to copy it in to the new array. I understand that,but i don't know how to use pointers to do it.so far i
have this

void stripWhite(char *str)
{
char str2[100];

char*p;
char *t;

}

any help would be great i need it asap.

Sep 27 '06 #1

Subscribe Reply

9 would i have to copy pointers? (urgent?) | Bytes (2) 2533 would i have to copy pointers? (urgent?) | Bytes (3)

Banfa

9,065 would i have to copy pointers? (urgent?) | Bytes (5) Recognized Expert Moderator Expert

You need to pointers,

1 to point at the current character to read from the source string.

1 to point at the current character to write to in the destination string.

Initally both pointers will point to the start of their respective arrays

then

Expand|Select|Wrap|Line Numbers

  1. FOREACHCharacterintheSourceString
  2. IFitisnotaSPACE
  3. Copyittothedestinationstring
  4. Incrementthedestinationstringpointer
  5. ENDIF
  6. Incrementthesourcestringpointer
  7. ENDFOR

Don't forget to write a terminator to the destination string.

Sep 27 '06 #2

reply

vermarajeev

180 would i have to copy pointers? (urgent?) | Bytes (6) New Member

Hi,
I have a project where i must use pointers and write a function that strips all the white spacebar spaces out of an array. I also have to count how many white spaces i have. Well i figured out how to count all the white spaces i just don't know how to strip all the white spaces out of the sentence entered. I asked the proffessor and he said one way of doing it would be to crreate another array and copy it to the new array and when it hits a white space not to copy it in to the new array. I understand that,but i don't know how to use pointers to do it.so far i
have this

void stripWhite(char *str)
{
char str2[100];

char*p;
char *t;

}

any help would be great i need it asap.

Expand|Select|Wrap|Line Numbers

  1. voidstripWhite(char*str)
  2. {
  3. intj=0;
  4. for(inti=0;str[i]!='\0';++i)
  5. {
  6. if(str[i]!='')
  7. str[j++]=str[i];
  8. }
  9. str[j]='\0';
  10. cout<<str<<endl;
  11. }

Sep 27 '06 #3

reply

billyjay777

7 would i have to copy pointers? (urgent?) | Bytes (7) New Member

Expand|Select|Wrap|Line Numbers

  1. voidstripWhite(char*str)
  2. {
  3. intj=0;
  4. for(inti=0;str[i]!='\0';++i)
  5. {
  6. if(str[i]!='')
  7. str[j++]=str[i];
  8. }
  9. str[j]='\0';
  10. cout<<str<<endl;
  11. }

That works but i have to use pointers.

Sep 27 '06 #4

reply

Banfa

9,065 would i have to copy pointers? (urgent?) | Bytes (9) Recognized Expert Moderator Expert

That works but i have to use pointers.

Adapt it

remember that

array[4]

is equivilent to

*(array+4)

Sep 27 '06 #5

billyjay777

7 would i have to copy pointers? (urgent?) | Bytes (10) New Member

Adapt it

remember that

array[4]

is equivilent to

*(array+4)

i am having a hard time understanding how to increment both pointers. I understand that they both start at the same point in there arrays, but i don't understand how to increment them to go to the next character in the array on the first array, but then tell the second array to skip the space.
that is the hardest thing i am dealing with so far i have this now.

void stripWhite(char * str)
{
char str2[100];
char *p;
p=str;
char*t;
t=str2;

{
if(*(p)!=' ')

{
t=p;

}

}
cout<<t<<endl;

}

Sep 27 '06 #6

reply

D_C

293 would i have to copy pointers? (urgent?) | Bytes (11) Contributor

What's so difficult? Always increment the pointer to the string with spaces. If you come upon a space, increment the number of spaces encountered. Otherwise, increment the pointer to the string without spaces.

If you are doing the operation in place, for some reason, then the number of spaces encountered is (ptr_with_space s - ptr_no_spaces).

Sep 28 '06 #7

reply

ltgbau

41 would i have to copy pointers? (urgent?) | Bytes (12) New Member

answer here:

void stripWhite(char * str)
{
char str2[100]="";
char *p;
p=str;
char*t;
t=str2;

int len=strlen(str) ;
int i=0;
int j=0;
while(i<len)
{
if(*(p+i)!=' ')
{
*(t+j)=*(p+i);
j++;
}
i++;
}
cout<<t<<endl;
}

Sep 28 '06 #8

reply

vermarajeev

180 would i have to copy pointers? (urgent?) | Bytes (13) New Member

i am having a hard time understanding how to increment both pointers. I understand that they both start at the same point in there arrays, but i don't understand how to increment them to go to the next character in the array on the first array, but then tell the second array to skip the space.
that is the hardest thing i am dealing with so far i have this now.

void stripWhite(char * str)
{
char str2[100];
char *p;
p=str;
char*t;
t=str2;

{
if(*(p)!=' ')

{
t=p;

}

}
cout<<t<<endl;

}

Hi, I think according to your question it just tells me to use pointers and as what I have posted there is a pointer too (i.e char* str). Also as Banfa said
arr[4] is equivalent to *(arr+4) that too is a pointer. So dont get panic about pointers. Use pointers whenever required. In your question it is never said that you have to use two pointers one for source and the other for destination.... .I just tells me to use pointers....

Try something which is simple and the best solution.
Thankx

Sep 28 '06 #9

reply

billyjay777

7 would i have to copy pointers? (urgent?) | Bytes (14) New Member

Hi, I think according to your question it just tells me to use pointers and as what I have posted there is a pointer too (i.e char* str). Also as Banfa said
arr[4] is equivalent to *(arr+4) that too is a pointer. So dont get panic about pointers. Use pointers whenever required. In your question it is never said that you have to use two pointers one for source and the other for destination.... .I just tells me to use pointers....

Try something which is simple and the best solution.
Thankx

sorry for not stating right. And thanks for the help.

Sep 28 '06 #10

reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 2776

How do copy Strings from a single dimensional array to double dimensional array

by: Venkat |last post by:

Hi All, I need to copy strings from a single dimensional array to a double dimensional array. Here is my program. #include <stdio.h> #include <stdlib.h>

C / C++

1 2489

composition/aggregation: would like to use constructor body rather than initializer list

by: Chris K |last post by:

I am relatively new to C++ and hope that this question is relevant. I have spent some time at the local library and some time on dejanews, but have no decided to go ahead with my question, since I found no satisfactory answer yet. It is about composed/aggregated classes. I am developing a scientific code (Monte Carlo) in which I find it natural to have classes with several levels of aggregation.

C / C++

9 9893

vector with deep copy

by: Gunnar G |last post by:

Is there anything like vector in STL, that performes deep copy of the elements it contains? I hope this will appear in future releases of STL :)

C / C++

2 12348

is dict.copy() a deep copy or a shallow copy

by: Alex |last post by:

Entering the following in the Python shell yields >>> help(dict.copy) Help on method_descriptor: copy(...) D.copy() -> a shallow copy of D >>>

Python

11 7394

How to avoid the use of copy constructor when using STL container function (push_back, etc.)

by: PengYu.UT |last post by:

The following program calls the normal constructor and the copy constructor. By calling the copy constuctor is redundandant, all I want is only a vector of a trial object. Is there any way to avoid the use of the copy constructor? #include <vector> #include <iostream>

C / C++

3 6466

TCP Urgent Pointer Usage

by: N. Spiker |last post by:

I am attempting to receive a single TCP packet with some text ending with carriage return and line feed characters. When the text is send and the packet has the urgent flag set, the text read from the socket is missing the last character (line feed). When the same text is sent without the urgent flag set, all of the characters are read. I'm reading the data using the blocking read call of the network stream class. The .NET...

.NET Framework

5 1694

Experimental only: Pointer copy consturctor does not work

by: nagrik |last post by:

Hello group, Last week I picked up a thread, which pointed out that if a copy constructor is created with pointers instead of reference, there is a danger of it going in infinite recursion. My observation: 1. Compiler does not complain.

C / C++

4 3145

How to implement a deep copy or shallow copy?

by: shuisheng |last post by:

Dear All, Is there any easy way to make sure all my object copies are deep copy or shallow copy? I do not like to implement it in each class one by one. Thanks, Shuisheng

C / C++

1 1580

dictionary.copy()?

by: 7stud |last post by:

Here is some example code: d = {"a":"hello", "b":} x = d.copy() d=10 print x output: {'a': 'hello', 'b': }

Python

8595

Changing the language in Windows 10

by: Hystou |last post by:

Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...

Windows Server

9142

Problem With Comparison Operator <=> in G++

by: Oralloy |last post by:

Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...

C / C++

1 8875

The easy way to turn off automatic updates for Windows 10/11

by: Hystou |last post by:

Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...

Windows Server

8849

Discussion: How does Zigbee compare with other wireless protocols in smart home applications?

by: tracyyun |last post by:

Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...

General

7699

AI Job Threat for Devs

by: agi2029 |last post by:

Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...

Career Advice

1 6512

Access Europe - Using VBA to create a class based on a table - Wed 1 May

by: isladogs |last post by:

The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...

Microsoft Access / VBA

5855

Couldn’t get equations in html when convert word .docx file to html file in C#.

by: conductexam |last post by:

I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...

C# / C Sharp

4608

Windows Forms - .Net 8.0

by: adsilva |last post by:

A Windows Forms form does not have the event Unload, like VB6. What one acts like?

Visual Basic .NET

1 3030

transfer the data from one system to another through ip address

by: 6302768590 |last post by:

Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

C# / C Sharp

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisem*nts and analytics tracking please visit the page.

would i have to copy pointers? (urgent?) | Bytes (2024)

FAQs

Are pointers copied when passed as an argument? ›

Pass-by-pointer means to pass a pointer argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the variable to which the pointer argument points. When you use pass-by-pointer, a copy of the pointer is passed to the function.

Is it necessary to use pointers? ›

Pointers are absolutely necessary for a systems language like c++. Pointers provide efficency that wouldn't be possible any other way. If you get rid of pointers you might as well get rid of functions too since the cost of passing data between functions would become ridiculously expensive.

Can I copy a pointer? ›

In C++, copying a pointer to another pointer can be done by assigning the value of one pointer to another. For example, if you have two pointers `ptr1` and `ptr2`, you can copy the value of `ptr1` to `ptr2` with the assignment `ptr2 = ptr1;`.

Why do I need a pointer to a pointer? ›

The pointer to a pointer in C is used when we want to store the address of another pointer. The first pointer is used to store the address of the variable.

Can shared pointers be copied? ›

The shared_ptr type is a smart pointer in the C++ standard library that is designed for scenarios in which more than one owner needs to manage the lifetime of an object. After you initialize a shared_ptr you can copy it, pass it by value in function arguments, and assign it to other shared_ptr instances.

Can unique pointers be copied? ›

These examples demonstrate this basic characteristic of unique_ptr : it can be moved, but not copied.

Why avoid pointers? ›

The misuse of pointers is a major source of bugs: the constant allocation, deallocation and referencing that must be performed by a program written using pointers introduces the risk that memory leaks will occur.

Why are pointers difficult? ›

Understanding pointers is often regarded as one of the most challenging aspects of learning the C programming language. Many individuals struggle to grasp the concept of pointers due to the inherent complexity of comprehending how the central processing unit (CPU) and memory work together.

What are the disadvantages of pointers? ›

Drawbacks of Pointers:

If pointers are pointed to some incorrect location then it may end up reading a wrong value. Erroneous input always leads to an erroneous output. Segmentation fault can occur due to uninitialized pointer.

What is an illegal pointer? ›

That means a pointer initialized as an integer can only store an address of integer datatype. So when used for pointing(storing address) another datatype than initialized datatype illegal pointer error can occur.

What happens when you assign a pointer to another pointer? ›

Pointer assignment between two pointers makes them point to the same pointee. So the assignment y = x; makes y point to the same pointee as x . Pointer assignment does not touch the pointees. It just changes one pointer to have the same reference as another pointer.

How do you manipulate a pointer? ›

A pointer can be incremented by value or by address based on the pointer data type. For example, an integer pointer can increment memory address by 4, since the integer takes up 4 bytes.

Are pointers really needed? ›

They allow programmers to work with memory directly, enabling efficient memory management and more complex data structures. By using pointers, you can access and modify data located in memory, pass data efficiently between functions, and create dynamic data structures like linked lists, trees, and graphs.

Why pointers are not used? ›

The use of raw pointers is not recommended because it's too easy to lose track of them and never release the memory they reference. The result is a memory leak - memory that is acquired but never released. At best, a memory leak turns a running program into a memory hog that consumes a computer's resources.

How important are pointers in C? ›

Pointers are one of the most important and powerful features of the C programming language. They allow us to manipulate memory directly, which can be very useful in many programming scenarios. In C, a pointer is simply a variable that holds a memory address.

What happens when an object is passed as an argument? ›

Absolutely nothing happens to the object. A number (pointer/reference) is passed that tells the method being invoked how to locate the object. That method can do whatever it is allowed for it to do with an object, thanks to it's pointer/reference. So, by just “invoking a method” nothing happens.

What happens when a pointer is passed to a function? ›

A pointer to a function is passed in this example. As an argument, a pointer is passed instead of a variable and its address is passed instead of its value. As a result, any change made by the function using the pointer is permanently stored at the address of the passed variable.

Can a pointer to a structure be passed as a function argument? ›

A structure can be transmitted to another function using a pointer which as we have seen above implements the call by reference scheme. The address of the structure transmitted by the calling function is received in the called function in a pointer to the same type of structure.

When an argument is passed by only a copy? ›

When only a copy of an argument is passed to a function, it is said to be passed by "value". So the missing term is "value".

Top Articles
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 5289

Rating: 4.1 / 5 (72 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.