C - Acces参数向量创建指针&读取价值

时间:2017-02-02 16:03:59

标签: c pointers

我正在尝试创建参数向量的指针并从中读取值。
这没有按预期工作。这是当前的代码

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


int main(int argc, char *argv[])
{
   char *ptr_to_a;

   if(argc != 3)
   {
      return -1;
   }

   char* argumentA = argv[1];
   char* argumentB = argv[2];

   ptr_to_a = &argumentA;


   printf("First argument is %s \n", argumentA);
   printf("Second argument is %s \n", argumentB);

   printf("Memory address of ptr_to_a is %p \n", ptr_to_a);
   printf("Memory address of argumentA is %p \n", &argumentA);

   printf("First argument through pointer is %s \n", *ptr_to_a);



   return 0;
}

此代码的输出如下

First argument is hello 
Second argument is world 
Memory address of ptr_to_a is 0x7fff5744cb80 
Memory address of argumentA is 0x7fff5744cb80 
First argument through pointer is ? 

我做错了什么:?

1 个答案:

答案 0 :(得分:4)

&argumentA是指向char**的指针(ptr_to_a)的指针,char*ptr_to_a = &argumentA; 。因此,此作业中存在类型不匹配:

ptr_to_a

char**的类型更改为 char **ptr_to_a;

using System;
using System.Web;
using System.Web.UI;
using TikaOnDotNet.TextExtraction;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using Excel;
using System.Data;
using System.Collections.Generic;

namespace Project
{

    public partial class Default : System.Web.UI.Page
    {
        int y;
        string[] res = null;

        public void button1Clicked(object sender, EventArgs args)
        {
            button1.Text = "You clicked me";
            DPdfView v = new DPdfView();
            res = v.ReadCSV().ToArray();
            generatepdffile(res);


        }

        protected void generatepdffile(string[] res)
        {

            Document doc = new Document();

            PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("file9.pdf"), FileMode.Create));
            doc.Open();
            PdfPTable table = new PdfPTable(7);

            PdfPCell c1 = new PdfPCell();
            PdfPCell c2 = new PdfPCell();
            PdfPCell c3 = new PdfPCell();
            PdfPCell c4 = new PdfPCell();
            PdfPCell c5 = new PdfPCell();
            PdfPCell c6 = new PdfPCell();
            PdfPCell c7 = new PdfPCell();
            String con = null;
            for (int i= 0;i<res.Length;i++)
            {
                List<String> col = new List<String>();

                foreach (string line in res[i].ToString().Split('|'))
                {
                    con = res[i];
                    col.Add(line);
                }

                string[] columns = col.ToArray();

                    c1.AddElement(new Phrase(columns[0]));
                    c2.AddElement(new Phrase(columns[1]));
                    c3.AddElement(new Phrase(columns[2]));
                    c4.AddElement(new Phrase(columns[3]));
                    c5.AddElement(new Phrase(columns[4]));
                    c6.AddElement(new Phrase(columns[5]));
                    c7.AddElement(new Phrase(columns[6]));

                table.Rows.Add(new PdfPRow(new PdfPCell[] { c1, c2, c3, c4, c5, c6, c7 }));



            }
            AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, table);
            AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, new Chunk(con));

            doc.Close();
        }

        private void AddParagraph(Document doc, int alignment, iTextSharp.text.Font font, iTextSharp.text.IElement content)
        {
            Paragraph paragraph = new Paragraph();
            paragraph.SetLeading(0f, 1.2f);
            paragraph.Alignment = alignment;
            paragraph.Font = font;
            paragraph.Add(content);
            doc.Add(paragraph);
        }

        private Font _largeFont = new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD, BaseColor.BLACK);

    }
}