473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C strings and pointers mitola

17
C program language:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <iostream>
  5.  
  6.  
  7. int main()
  8. {char a[100],b[100],*pa,*pb;
  9. pa = &a; pb = &b;
  10.     scanf("%s",*pa);
  11.     scanf("%s",*pb);
  12.     printf("vnesi 2 niza / input 2 strings");
  13.     printf("\nstring: %s",*pa);
  14.     printf("string: %s",*pb); 
  15.     system("pause");
  16.     return 0;
  17. }
  18.  
well i dont know how to connect string and a pointer

pa = &a; pb = &b; i presume that problem is only here but i cant corect it.
Oct 13 '07 #1
3 1232
ilikepython
844 Expert 512MB
C program language:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>



int main()
{char a[100],b[100],*pa,*pb;
pa = &a; pb = &b;
scanf("%s",*pa);
scanf("%s",*pb);
printf("vnesi 2 niza / input 2 strings");
printf("\nstring: %s",*pa);
printf("string: %s",*pb);
system("pause");
return 0;
}


well i dont know how to connect string and a pointer

pa = &a; pb = &b; i presume that problem is only here but i cant corect it.
Ok, A and B are already pointers so you don't have to get the addresses. If you want a pointer to a char array, you need a pointer to a pointer.
Oct 14 '07 #2
Banfa
9,065 Expert Mod 8TB
Ok, A and B are already pointers so you don't have to get the addresses. If you want a pointer to a char array, you need a pointer to a pointer.
No on the whole this is rather wrong!
Oct 14 '07 #3
Banfa
9,065 Expert Mod 8TB
This is not wrong
Expand|Select|Wrap|Line Numbers
  1. pa = &a; pb = &b;
but this would be more conventional
Expand|Select|Wrap|Line Numbers
  1. pa = a; pb = b;
Generally speaking for an array a as you have defined it the a == &a, the only difference is the type of the expression.

However you 1st actual problem is in your scanf lines
Expand|Select|Wrap|Line Numbers
  1.     scanf("%s",*pa);
  2.     scanf("%s",*pb);
  3.  
%s tells scanf to expect a pointer to char but you pass a char, *pa these should be
Expand|Select|Wrap|Line Numbers
  1.     scanf("%s",pa);
  2.     scanf("%s",pb);
  3.  
And then you make the same mistake in your printf calls
Expand|Select|Wrap|Line Numbers
  1.     printf("\nstring: %s",*pa);
  2.     printf("string: %s",*pb); 
  3.  
should be
Expand|Select|Wrap|Line Numbers
  1.     printf("\nstring: %s",pa);
  2.     printf("string: %s",pb); 
  3.  
However on the whole in this program pa and pb are completely superfluous, you could get rid of them and replace them with a and b.
Oct 14 '07 #4

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

Similar topics

11
by: Michael B. Allen | last post by:
Coming from C and Java on *nix I'm a little out of my element messing around with CList and MSVC++ but I think my issues are largely syntactic. I have an ADT that I use called a 'varray' that can...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
10
by: Ian Todd | last post by:
Hi, I am trying to read in a list of data from a file. Each line has a string in its first column. This is what i want to read. I could start by saying char to read in 1000 lines to the array( i...
25
by: Rainmaker | last post by:
Hi, Can anyone tell me an efficient algorithm to sort an array of strings? Keep in mind that this array is HUGE and so the algorithm should me efficient enough to deal with it. Thanks
9
by: Steven | last post by:
Hello, I have a question about strcmp(). I have four words, who need to be compared if it were two strings. I tried adding the comparison values like '(strcmp(w1, w2) + strcmp(w3, w4))', where...
10
by: Aris | last post by:
A few days ago I asked from you how to join a string like "file" A number that change values from 1 to 5 and another string like ".txt" to have a result like "file1.txt","file2.txt" and so on ...
9
by: Dadio | last post by:
Hi! I have to take some strings from a file and put them in a record... The various strings in the file are written on this way: string1|string2|string3|string4|string5| This is the program...
4
by: CoreyWhite | last post by:
/* WORKING WITH STRINGS IN C++ IS THE BEST WAY TO LEARN THE LANGUAGE AND TRANSITION FROM C. C++ HAS MANY NEW FEATURES THAT WORK TOGETHER AND WHEN YOU SEE THEM DOING THE IMPOSSIBLE AND MAKING...
16
by: InDepth | last post by:
Now that .NET is at it's fourth release (3.5 is coming soon), my very humble question to the gurus is: "What have we won with the decision to have string objects immutable? Or did we won?" ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.